使用 Spring Boot 来实现 Spring Security
我试了一下,稍微有些问题,因为”Getting Started”部分有些缺陷,我记下来了。
STS的安装。
安装3.5.1版本
https://spring.io/tools/sts
(如果想使用gradle,则可以在仪表盘的IDE EXTENSIONS中安装Gradle插件)
开始项目的创建

试试启动。

确认管理者页面
package hello;
@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
//管理者ページの追加
.antMatchers("/admin").hasRole("ADMIN")
.anyRequest().authenticated();
http
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
//管理者ユーザーの登録
auth
.inMemoryAuthentication()
.withUser("admin").password("password").roles("ADMIN");
}
}
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/home").setViewName("home");
registry.addViewController("/").setViewName("home");
registry.addViewController("/hello").setViewName("hello");
registry.addViewController("/login").setViewName("login");
//管理者ページの追加
registry.addViewController("/admin").setViewName("admin");
}
}
隨意添加模板頁面(java/main/resources/templates)。
<!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>Admin Page</title>
</head>
<body>
<h1 th:inline="text">Hello Administrator[[${#httpServletRequest.remoteUser}]]!</h1>
<form th:action="@{/logout}" method="post">
<input type="submit" value="Sign Out"/>
</form>
</body>
</html>
我试试看。
使用「用户」登录,进入localhost:8080/admin时出现错误。

使用「admin」进行登录,在 localhost:8080/admin 中正常显示。

通过标签控制显示
thymeleaf内置了专门用于Spring Security的方言。
http://www.thymeleaf.org/springsecurity.html
<!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>Hello World!</title>
</head>
<body>
<div sec:authorize="hasRole('ROLE_ADMIN')">This is Admin</div>
<div sec:authorize="hasRole('ROLE_USER')">This is User</div>
<h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>
<form th:action="@{/logout}" method="post">
<input type="submit" value="Sign Out"/>
</form>
</body>
</html>
这应该是正确的,但不知为何,sec: 在 HTML 中仍然直接显示,标签的显示控制无法实施。
请在 build.gradle 文件中添加以下内容。
参考链接:http://stackoverflow.com/questions/22606311/filter-html-content-with-spring-and-thymeleaf
dependencies {
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-starter-security")
//追加
compile ('org.thymeleaf.extras:thymeleaf-extras-springsecurity3:2.1.1.RELEASE')
//これは不備で、本来は下記のようになるはず
//compile ('org.thymeleaf.extras:thymeleaf-extras-springsecurity4:バージョン')
testCompile("junit:junit")
}
可以使用这个来控制标签的显示。
补充说明。正如评论所指出的那样,模板的ns声明和dependencies声明不一致。org.thymeleaf.extras在Spring3和4中是分开的,这本来就是一个错误。在修正之后,我还没有进行过验证。非常抱歉。