Spring Boot SpringSecurity5 身份验证
对于没有访问权限的用户需要转到登录表单页面。要实现访问控制的方法多种多样,可以通过Aop、拦截器实现,也可以通过框架实现(如:Apache Shiro、Spring Security)。
pom.xml添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency></br> <groupId>org.springframework.boot</groupId></br> <artifactId>spring-boot-starter-thymeleaf</artifactId></br> </dependency></br> <dependency></br> <groupId>org.springframework.boot</groupId></br> <artifactId>spring-boot-starter-security</artifactId></br> </dependency></pre>
创建SpringSecurity配置类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import
org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import
org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Override</br> </span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">void</span> configure(HttpSecurity http) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception {</br> http</br> .authorizeRequests()</br> .antMatchers(</span>"/", "/home"<span style="color: #000000;">).permitAll()</br> .anyRequest().authenticated()</br> .and()</br> .formLogin()</br> .loginPage(</span>"/login"<span style="color: #000000;">)</br> .permitAll()</br> .and()</br> .logout()</br> .permitAll();</br> }</br> @Autowired</br> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> configureGlobal(AuthenticationManagerBuilder auth) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception {</br> auth</br> .inMemoryAuthentication()</br> .withUser(</span>"admin").password("123456").roles("USER"<span style="color: #000000;">);</br> }</br>
}
通过@EnableWebSecurity注解开启Spring Security的功能
继承WebSecurityConfigurerAdapter,并重写它的方法来设置一些web安全的细节
configure(HttpSecurity http)方法,通过authorizeRequests()定义哪些URL需要被保护、哪些不需要被保护。例如以上代码指定了/和/home不需要任何认证就可以访问,其他的路径都必须通过身份验证。
通过formLogin()定义当需要用户登录时候,转到的登录页面。
configureGlobal(AuthenticationManagerBuilder auth)方法,在内存中创建了一个用户,该用户的名称为admin,密码为123456,用户角色为USER。
控制器:
@Controller public class HelloController {
@RequestMapping(</span>"/"<span style="color: #000000;">)</br> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String index() {</br> </span><span style="color: #0000ff;">return</span> "index"<span style="color: #000000;">;</br> }</br> @RequestMapping(</span>"/hello"<span style="color: #000000;">)</br> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String hello() {</br> </span><span style="color: #0000ff;">return</span> "hello"<span style="color: #000000;">;</br> }</br></br> @RequestMapping(value </span>= "/login", method =<span style="color: #000000;"> RequestMethod.GET)</br> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String login() {</br> </span><span style="color: #0000ff;">return</span> "login"<span style="color: #000000;">;</br> }</br></br>
}
index.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 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>
<form th:action="@{/logout}" method="post">
<input type="submit" value="注销"/>
</form>
</body>
</html>
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>
运行:
打开index.html,点击这里,如果没有登录进入登录页,已登录跳转到hello.html