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?
spring spring-mvc spring-security thymeleaf
add a comment |
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?
spring spring-mvc spring-security thymeleaf
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
add a comment |
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?
spring spring-mvc spring-security thymeleaf
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
spring spring-mvc spring-security thymeleaf
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
add a comment |
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
add a comment |
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.
I've tried and I get the same result
– Jane C.
Oct 18 at 14:47
add a comment |
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>
add a comment |
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.
I've tried and I get the same result
– Jane C.
Oct 18 at 14:47
add a comment |
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.
I've tried and I get the same result
– Jane C.
Oct 18 at 14:47
add a comment |
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.
Have you try using the hasAuthority instead of hasRole?
sec:authorize="hasAuthority('ADMIN')"
It seems, that hasRole doesn't work for Spring 4.
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
add a comment |
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
add a comment |
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>
add a comment |
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>
add a comment |
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>
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>
edited Nov 11 at 9:54
answered Nov 10 at 22:20
skara
12
12
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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