Spring Security
准备工作
- 创建一个 Web 工程,需要引入
spring-boot-starter-web
的包 - 引入
spring-boot-starter-security
的包 - 引入 thymeleaf 的包
spring-boot-starter-thymeleaf
(实测,如果不引入 thymeleaf,则无法从控制器请求到 html 视图)
Web 层实现请求映射
@Controller
public class HelloController {
@RequestMapping("/")
public String index() {
return "index"; // 映射到 index.html
}
@RequestMapping("/hello")
public String hello() {
return "hello"; // 映射到 hello.html
}
}
实现映射的页面
映射的页面统一放在 src/main/resources/teamplates
下
- index.html
在该页面中,通过链接直接请求到 hello 方法,并通过 hello 映射到 hello.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-springsecurity3">
<head>
<title>Spring Security入门</title>
</head>
<body>
<h1>欢迎使用Spring Security!</h1>
<p>点击 <a th:href="@{/hello}">这里</a> 打个招呼吧</p>
</body>
</html>
- hello.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-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello world!</h1>
</body>
</html>
整合 Spring Security
这里会对 /hello
请求进行权限控制,必须是授权用户才能访问。如果是未授权用户,则直接跳转到登录页面。
配置 Spring Security
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll() // "/"、"/home" 不需要被验证
.anyRequest().authenticated() // 其他路径的请求都需要验证
.and()
.formLogin()
.loginPage("/login") // 指定登录页面,谁都可以访问
.permitAll()
.and()
.logout() // 登出页面不需要验证
.permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
- 通过 @EnableWebSecurity 注解开启 Spring Security
- 继承 WebSecurityConfigurerAdapter ,并通过重写方法来设置一些安全细节
- configure(HttpSecurity http) 方法
- 通过 authorizeRequests() 定义哪些URL需要被保护、哪些不需要被保护。
- 通过 formLogin() 定义当需要用户登录时候,转到的登录页面。并且通过 loginPage(String) 的方法指定登录页面
- configureGlobal(AuthenticationManagerBuilder auth) 方法,在内存中创建了一个用户,该用户的名称为 user,密码为 password,用户角色为 USER。
新增登录请求与页面
- login.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-springsecurity3">
<head>
<title>Spring Security Example </title>
</head>
<body>
<div th:if="${param.error}">
用户名或密码错
</div>
<div th:if="${param.logout}">
您已注销成功
</div>
<form th:action="@{/login}" method="post">
<div><label> 用户名 : <input type="text" name="username"/> </label></div>
<div><label> 密 码 : <input type="password" name="password"/> </label></div>
<div><input type="submit" value="登录"/></div>
</form>
</body>
</html>
- /login
@Controller
public class HelloController {
// 省略之前的内容...
@RequestMapping("/login")
public String login() {
return "login";
}
}
根据配置,Spring Security 提供了一个过滤器来拦截请求并验证用户身份。
如果用户身份验证失败,页面就会重定向到 /login?error
,并且页面中会展现相应的错误信息。
若用户想要注销登录,可以通过访问 /login?logout
请求,在完成注销之后,页面展现相应的成功消息。
因为 /hello
请求需要验证用户信息,因此在未登录状态下被重定向到 /login
方法中,通过用户名与密码登录之后,就会跳转到 /hello
页面。
网友评论