springboot集成Security
如果所示,我们的security的工作流程。我们只需要去定义我们自己的WebSecurityConfigurerAdapter并且重写两个核心的配置方法,就可以使用Secutiry进行我们的安全控制工作。
- Security是通过拦截器的思想来完成我们的工作的,我们也可以通过拦截器来完成我们类似的工作。拦截所有的请求,进行用户校验和请求放行。
使用前置(导入依赖)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
通过一个Demo来模拟这个过程。
-
前端页面发送一个请求
<a class="item" th:href="@{/level1/1}"> <i class="address card icon"></i> 登录 </a>
-
拦截请求并且处理
想要拦截请求,我们必须配置我们的Security
@EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { //授权 @Override protected void configure(HttpSecurity http) throws Exception { //首页所有人可以访问,但是功能也只有对应有权限的人可以访问。 System.out.println("页面所需规则"); //请求授权的规则。 //一些请求必须要有对应权限才可以映射到接口。 http.authorizeRequests() .antMatchers("/").permitAll() //"/"请求允许用户所有进入 .antMatchers("/level1/**").hasRole("vip1") //"/level1/"下的所有请求需要vip1用户权限才可以进入 .antMatchers("/level2/**").hasRole("vip2") .antMatchers("/level3/**").hasRole("vip3"); http.csrf().disable(); //关闭csrf功能(跨站伪域请求),不然除get方法的其他方法失效,若是不想关闭,可以参考官方文档的使用token验证。 //没有权限,跳转至对应接口 //自定义登录页面,设置密码验证,自定义用户名和密码属性,自定义跳转请求。 http.formLogin().loginPage("/toLogin") //跟着接口绑定登录页面 .usernameParameter("username") //绑定登录页面设置的用户名,方便后续直接使用 .passwordParameter("password") .failureUrl("/error1") //密码或用户名错误时跳转 .loginProcessingUrl("/login"); //设置请求接口与前端提交适配,前端点击这个请求就会去适配认证 //权限不足跳转?自定义错误页面即可 //开启记住我 http.rememberMe().rememberMeParameter("remember"); //开启注销功能 http.logout().logoutSuccessUrl("/"); } //认证 springboot2.1.x 可以直接使用。 //密码编码。 //在spring5.0+中,新增了很多加密方式。 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { System.out.println("验证用户密码,授予用户权限"); //正常应该从数据库读取。 //思想是每个用户都应该有一个权限表,根据权限表为用户赋予权限。 //并且密码验证也在这做 auth.inMemoryAuthentication() .passwordEncoder(new BCryptPasswordEncoder()) .withUser("lyj").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3") .and() .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3") .and() .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1"); } }
配置了之后所有请求都被会被拦截下来了。
它配置了请求需要某些权限才可以被映射到接口,以及做了一些密码验证和权限授予
-
拦截下来的请求过滤一下规则链判断当前用户是否有权限
- 若不是第一次登录,有对应权限,放行
- 若第一此登录,无权限跳转到指定登录页面(若不指定,跳转至默认登录页面)
//有对应权限直接放行 http.authorizeRequests() .antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("vip1") .antMatchers("/level2/**").hasRole("vip2") .antMatchers("/level3/**").hasRole("vip3"); //无对应权限,跳转至对应接口 http.formLogin().loginPage("/toLogin") .usernameParameter("username") .passwordParameter("password") .loginProcessingUrl("/login");
-
定义跳转登录的接口
@RequestMapping("/toLogin") public String toLogin(){ System.out.println("我在进行登录"); return "views/login"; }
-
在登录页面views/login.html填写登录信息并提交(自定义表单)
<form action="/login"> //与配置中路径的对应 <input name="username" type="text"> <input name="password" type="password"> <input type="submit"/> </from>
-
Security开始对数据校验并授权
正常都是从数据库去取出这三个数据。
这里省略,只对三个用户进行授权和允许登录。
auth.inMemoryAuthentication() .passwordEncoder(new BCryptPasswordEncoder()) .withUser("lyj").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3") .and() .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3") .and() .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
-
再次去过滤连过滤
http.authorizeRequests() .antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("vip1") .antMatchers("/level2/**").hasRole("vip2") .antMatchers("/level3/**").hasRole("vip3");
-
权限通过,请求放行,允许被映射到接口
@RequestMapping("/level1/{id}") public String level1(@PathVariable("id") Integer id){ return "views/level1/"+id; }
-
登录成功