sec authorize it doesn't work on spring security











up vote
0
down vote

favorite












I'm trying to integrate my SpringMVC and Thymeleaf project with Spring Security. I found that this question is usual but I tried the solutions and no one works for me.



I added the org.thymeleaf.spring4.SpringTemplateEngine class to my configuration but it didn't work.



spring-security.xml



<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.2.xsd">


<http auto-config="true" use-expressions="true">
<intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />
</http>


<authentication-manager>
<authentication-provider>
<user-service>
<user name="usuario" password="123456" authorities="ROLE_USER" />
</user-service>
<password-encoder ref="passwordEncoder" />
</authentication-provider>
</authentication-manager>

<beans:bean id ="passwordEncoder"
class = "org.springframework.security.crypto.password.NoOpPasswordEncoder" factory-method = "getInstance" />

<beans:bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<beans:property name="additionalDialects">
<beans:set>
<beans:bean class="org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect"/>
</beans:set>
</beans:property>
</beans:bean>

</beans:beans>


Page:



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">


<head>
<title>Home</title>

</head>

<body

<div sec:authorize="hasRole('ROLE_USER')">Text visible to user.</div>
<div sec:authorize="hasRole('ROLE_ADMIN')">Text visible to admin.</div>
<div sec:authorize="isAuthenticated()">
Text visible only to authenticated users.
</div>
<h4>Spring security.</h4>

</body>

</html>


pom.xml



    <dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>

<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>


It show all this content to the users before you login it.
Please, can you help me?










share|improve this question






















  • Did you try this version thymeleaf-extras-springsecurity5?
    – Adina
    Oct 17 at 18:09










  • I can't because my project has the 4.3 version of spring
    – Jane C.
    Oct 17 at 18:49















up vote
0
down vote

favorite












I'm trying to integrate my SpringMVC and Thymeleaf project with Spring Security. I found that this question is usual but I tried the solutions and no one works for me.



I added the org.thymeleaf.spring4.SpringTemplateEngine class to my configuration but it didn't work.



spring-security.xml



<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.2.xsd">


<http auto-config="true" use-expressions="true">
<intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />
</http>


<authentication-manager>
<authentication-provider>
<user-service>
<user name="usuario" password="123456" authorities="ROLE_USER" />
</user-service>
<password-encoder ref="passwordEncoder" />
</authentication-provider>
</authentication-manager>

<beans:bean id ="passwordEncoder"
class = "org.springframework.security.crypto.password.NoOpPasswordEncoder" factory-method = "getInstance" />

<beans:bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<beans:property name="additionalDialects">
<beans:set>
<beans:bean class="org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect"/>
</beans:set>
</beans:property>
</beans:bean>

</beans:beans>


Page:



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">


<head>
<title>Home</title>

</head>

<body

<div sec:authorize="hasRole('ROLE_USER')">Text visible to user.</div>
<div sec:authorize="hasRole('ROLE_ADMIN')">Text visible to admin.</div>
<div sec:authorize="isAuthenticated()">
Text visible only to authenticated users.
</div>
<h4>Spring security.</h4>

</body>

</html>


pom.xml



    <dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>

<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>


It show all this content to the users before you login it.
Please, can you help me?










share|improve this question






















  • Did you try this version thymeleaf-extras-springsecurity5?
    – Adina
    Oct 17 at 18:09










  • I can't because my project has the 4.3 version of spring
    – Jane C.
    Oct 17 at 18:49













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to integrate my SpringMVC and Thymeleaf project with Spring Security. I found that this question is usual but I tried the solutions and no one works for me.



I added the org.thymeleaf.spring4.SpringTemplateEngine class to my configuration but it didn't work.



spring-security.xml



<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.2.xsd">


<http auto-config="true" use-expressions="true">
<intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />
</http>


<authentication-manager>
<authentication-provider>
<user-service>
<user name="usuario" password="123456" authorities="ROLE_USER" />
</user-service>
<password-encoder ref="passwordEncoder" />
</authentication-provider>
</authentication-manager>

<beans:bean id ="passwordEncoder"
class = "org.springframework.security.crypto.password.NoOpPasswordEncoder" factory-method = "getInstance" />

<beans:bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<beans:property name="additionalDialects">
<beans:set>
<beans:bean class="org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect"/>
</beans:set>
</beans:property>
</beans:bean>

</beans:beans>


Page:



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">


<head>
<title>Home</title>

</head>

<body

<div sec:authorize="hasRole('ROLE_USER')">Text visible to user.</div>
<div sec:authorize="hasRole('ROLE_ADMIN')">Text visible to admin.</div>
<div sec:authorize="isAuthenticated()">
Text visible only to authenticated users.
</div>
<h4>Spring security.</h4>

</body>

</html>


pom.xml



    <dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>

<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>


It show all this content to the users before you login it.
Please, can you help me?










share|improve this question













I'm trying to integrate my SpringMVC and Thymeleaf project with Spring Security. I found that this question is usual but I tried the solutions and no one works for me.



I added the org.thymeleaf.spring4.SpringTemplateEngine class to my configuration but it didn't work.



spring-security.xml



<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.2.xsd">


<http auto-config="true" use-expressions="true">
<intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />
</http>


<authentication-manager>
<authentication-provider>
<user-service>
<user name="usuario" password="123456" authorities="ROLE_USER" />
</user-service>
<password-encoder ref="passwordEncoder" />
</authentication-provider>
</authentication-manager>

<beans:bean id ="passwordEncoder"
class = "org.springframework.security.crypto.password.NoOpPasswordEncoder" factory-method = "getInstance" />

<beans:bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<beans:property name="additionalDialects">
<beans:set>
<beans:bean class="org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect"/>
</beans:set>
</beans:property>
</beans:bean>

</beans:beans>


Page:



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">


<head>
<title>Home</title>

</head>

<body

<div sec:authorize="hasRole('ROLE_USER')">Text visible to user.</div>
<div sec:authorize="hasRole('ROLE_ADMIN')">Text visible to admin.</div>
<div sec:authorize="isAuthenticated()">
Text visible only to authenticated users.
</div>
<h4>Spring security.</h4>

</body>

</html>


pom.xml



    <dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>

<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>


It show all this content to the users before you login it.
Please, can you help me?







spring spring-mvc spring-security thymeleaf






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Oct 17 at 17:55









Jane C.

107




107












  • Did you try this version thymeleaf-extras-springsecurity5?
    – Adina
    Oct 17 at 18:09










  • I can't because my project has the 4.3 version of spring
    – Jane C.
    Oct 17 at 18:49


















  • Did you try this version thymeleaf-extras-springsecurity5?
    – Adina
    Oct 17 at 18:09










  • I can't because my project has the 4.3 version of spring
    – Jane C.
    Oct 17 at 18:49
















Did you try this version thymeleaf-extras-springsecurity5?
– Adina
Oct 17 at 18:09




Did you try this version thymeleaf-extras-springsecurity5?
– Adina
Oct 17 at 18:09












I can't because my project has the 4.3 version of spring
– Jane C.
Oct 17 at 18:49




I can't because my project has the 4.3 version of spring
– Jane C.
Oct 17 at 18:49












2 Answers
2






active

oldest

votes

















up vote
0
down vote













Have you try using the hasAuthority instead of hasRole?



sec:authorize="hasAuthority('ADMIN')"


It seems, that hasRole doesn't work for Spring 4.






share|improve this answer





















  • I've tried and I get the same result
    – Jane C.
    Oct 18 at 14:47


















up vote
0
down vote













It is a matter of version.



Page:



xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"
:
<div sec:authorize="hasAuthority('ROLE_USER')">..</div>


pom.xml



<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52860933%2fsec-authorize-it-doesnt-work-on-spring-security%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Have you try using the hasAuthority instead of hasRole?



    sec:authorize="hasAuthority('ADMIN')"


    It seems, that hasRole doesn't work for Spring 4.






    share|improve this answer





















    • I've tried and I get the same result
      – Jane C.
      Oct 18 at 14:47















    up vote
    0
    down vote













    Have you try using the hasAuthority instead of hasRole?



    sec:authorize="hasAuthority('ADMIN')"


    It seems, that hasRole doesn't work for Spring 4.






    share|improve this answer





















    • I've tried and I get the same result
      – Jane C.
      Oct 18 at 14:47













    up vote
    0
    down vote










    up vote
    0
    down vote









    Have you try using the hasAuthority instead of hasRole?



    sec:authorize="hasAuthority('ADMIN')"


    It seems, that hasRole doesn't work for Spring 4.






    share|improve this answer












    Have you try using the hasAuthority instead of hasRole?



    sec:authorize="hasAuthority('ADMIN')"


    It seems, that hasRole doesn't work for Spring 4.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Oct 17 at 20:19









    Alain Cruz

    1,5211818




    1,5211818












    • I've tried and I get the same result
      – Jane C.
      Oct 18 at 14:47


















    • I've tried and I get the same result
      – Jane C.
      Oct 18 at 14:47
















    I've tried and I get the same result
    – Jane C.
    Oct 18 at 14:47




    I've tried and I get the same result
    – Jane C.
    Oct 18 at 14:47












    up vote
    0
    down vote













    It is a matter of version.



    Page:



    xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"
    :
    <div sec:authorize="hasAuthority('ROLE_USER')">..</div>


    pom.xml



    <dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
    <version>3.0.11.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity5</artifactId>
    <version>3.0.4.RELEASE</version>
    </dependency>





    share|improve this answer



























      up vote
      0
      down vote













      It is a matter of version.



      Page:



      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"
      :
      <div sec:authorize="hasAuthority('ROLE_USER')">..</div>


      pom.xml



      <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf-spring5</artifactId>
      <version>3.0.11.RELEASE</version>
      </dependency>
      <dependency>
      <groupId>org.thymeleaf.extras</groupId>
      <artifactId>thymeleaf-extras-springsecurity5</artifactId>
      <version>3.0.4.RELEASE</version>
      </dependency>





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        It is a matter of version.



        Page:



        xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"
        :
        <div sec:authorize="hasAuthority('ROLE_USER')">..</div>


        pom.xml



        <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring5</artifactId>
        <version>3.0.11.RELEASE</version>
        </dependency>
        <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        <version>3.0.4.RELEASE</version>
        </dependency>





        share|improve this answer














        It is a matter of version.



        Page:



        xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"
        :
        <div sec:authorize="hasAuthority('ROLE_USER')">..</div>


        pom.xml



        <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring5</artifactId>
        <version>3.0.11.RELEASE</version>
        </dependency>
        <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        <version>3.0.4.RELEASE</version>
        </dependency>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 9:54

























        answered Nov 10 at 22:20









        skara

        12




        12






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52860933%2fsec-authorize-it-doesnt-work-on-spring-security%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly