我正在使用Spring 4和Thymeleaf 在我的index.xhtml页面中,我写道:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
layout:decorator="layouts/layout">
<head>
<title>Welcome</title>
</head>
<body>
....
<div sec:authorize="hasRole('ROLE_ADMIN')">
You are authorized user! Hi, <span sec:authentication="name">Username</span>
</div>
<div sec:authorize="isAnonymous()">
You are NOT authorized user!
</div>
...
</body></html>
结果我看到了:
您是授权用户!嗨,用户名您不是授权用户!
即。 Spring Security无法运作
我的build.gradle(一些dependecies)是:
compile 'org.thymeleaf:thymeleaf-spring4:2.1.2.RELEASE'
compile 'org.thymeleaf.extras:thymeleaf-extras-springsecurity3:2.1.1.RELEASE'
compile 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:1.2.3'
compile 'org.springframework.security:spring-security-core:3.2.0.RELEASE'
compile 'org.springframework.security:spring-security-web:3.2.0.RELEASE'
compile 'org.springframework.security:spring-security-config:3.2.0.RELEASE'
compile 'org.springframework.security:spring-security-taglibs:3.2.0.RELEASE'
我的spring-security.xml是:
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Настройка хранилища безопасности -->
<authentication-manager>
<authentication-provider>
<password-encoder ref="bCryptPasswordEncoder">
</password-encoder>
<jdbc-user-service id="jdbcUserService" data-source-ref="dataSource"
users-by-username-query="select login, password, is_enabled from users where login = ?"
authorities-by-username-query="select u.login, p.`name`
from user_group_ref ug, permission_group_ref pg, users u, groups g, permissions p
where ug.user_id=u.id and ug.group_id=g.id and pg.group_id=g.id and pg.permission_id = p.id and u.login = ?"
group-authorities-by-username-query="select g.id, g.`name`, p.`name`
from user_group_ref ug, permission_group_ref pg, users u, groups g, permissions p
where ug.user_id=u.id and ug.group_id=g.id and pg.group_id=g.id and pg.permission_id = p.id and u.login = ?"
/>
</authentication-provider>
</authentication-manager>
<http use-expressions="true">
<!-- URLs на которых сработает интерцептор безопасности (permitAll - разрешить вход всем (в т.ч. анонимным)-->
<intercept-url pattern="/*" access='permitAll'/>
<!-- Настройка входа пользователя -->
<form-login login-page="/account/signin" authentication-failure-url="/account/login/fail"
username-parameter="login"
password-parameter="password"/>
<!-- Настройка выхода пользователя -->
<logout logout-url="/account/logout" />
<!-- Включает поддержку функции "Запомнить меня" -->
<remember-me remember-me-parameter="remember_me" user-service-ref="jdbcUserService"/>
</http>
<!-- Если указать этот файл в authentication-provider выше, то юзеры будут храниться в этом файле -->
<!-- <user-service id="userService"> -->
<!-- <user name="alexssource" authorities="ROLE_USER" password="123" /> -->
<!-- </user-service> -->
<!-- Хеширование паролей -->
<!--
При создании юзера используется так:
-> PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
-> String encodedPassword = passwordEncoder.encode(password);
-->
<beans:bean id='bCryptPasswordEncoder' class='org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder'/>
</beans:beans>
之前,我使用了Apache Tiles,因为它一切正常。 我不明白为什么Spring Secury不和Thymeleaf合作。 请帮助!
答案 0 :(得分:6)
我解决了这个问题。 我刚刚没有将SpringSecurityDialect添加到我的配置中。现在我的配置看起来像
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="thymeleafResolver" />
<property name="additionalDialects">
<set>
<bean class="nz.net.ultraq.thymeleaf.LayoutDialect" />
<bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"/>
</set>
</property>
</bean>
并且工作正常!
答案 1 :(得分:1)
@Bean
public SpringTemplateEngine templateEngine(){
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setEnableSpringELCompiler(true);
// add dialect spring security
templateEngine.addDialect(new SpringSecurityDialect());
return templateEngine;
}