1.注销功能实现
-
在spring security中同时帮我们实现了注销功能
-
记得我们怎么开启的用户登陆页吗?
http.formLogin();
-
在spring security中注销功能也只需要我们开启即可使用
http.logout();//开启注销功能
-
惯例:看源码,通过看源码我们可以发现这个方法本身实现没有什么参考价值,它的注释是重点
* Provides logout support. This is automatically applied when using * {@link WebSecurityConfigurerAdapter}. The default is that accessing the URL * "/logout" will log the user out by invalidating the HTTP Session, cleaning up any * {@link #rememberMe()} authentication that was configured, clearing the * {@link SecurityContextHolder}, and then redirect to "/login?success". * 当我们自定义了一个继承WebSecurityConfigurerAdapter的config类之后,就可以使用 * spring security提供的注销功能支持;默认配置为当你请求"/logou"的时候,将会将当前登陆的用户 * 注销,并且销毁本次会话使用的Session对象,清理所有被设置了"记住我"的认证,并清理 * 安全上下文管理器,最后重定向到"/login?success" * * <h2>Example Custom Configuration</h2> * 除了使用上面默认的配置,我们还可以定制自己的配置 * * The following customization to log out when the URL "/custom-logout" is invoked.// * Log out will remove the cookie named "remove", not invalidate the HttpSession, * clear the SecurityContextHolder, and upon completion redirect to "/logout-success". * 下面这个例子就是定了自己的配置 * 当我们请求自定义的url"/custom-logout"时,注销功能将会清楚名为"remove"的cookie,但是不会注销 * 本次会话的session对象,并会清理安全上下文管理器,最后将页面重定向到"/logout-success"页面 * * 上面自定义注销功能的代码实现 * @Configuration * @EnableWebSecurity * public class LogoutSecurityConfig extends WebSecurityConfigurerAdapter { * * @Override * protected void configure(HttpSecurity http) throws Exception { * http.authorizeRequests().antMatchers("/**";).hasRole("USER").and().formLogin() * .and()// sample logout customization * .logout().deleteCookies("remove";).invalidateHttpSession(false) * .logoutUrl("/custom-logout").logoutSuccessUrl("/logout-success"); * }
-
从注释中我们可以发现,在使用默认的注销功能的情况下,只要我们请求"/logout",就会注销当前已经登陆的、用于spring security用户认证的账户
-
所以,用户登陆和注销都从原来系统的功能需求被抽取出来,直接在spring security中通过调用两个方法就实现了
-
而所谓的登陆也是在完整spring security的用户认证,注销功能注销的也是spring security为了实现用户认证而登陆的用户账户,这一切都是spring security已经帮我们实现好了的,我们只是拿过来用就行了
-
使用step1:前端添加注销a链接
<a class="item" th:href="@{/logout}"> <i class="sign-out icon"></i> 注销 </a>
-
测试
-
此时我们并没有登陆用户进行"认证",所以我们在首页中请求任意一个资源都会重定向到登陆页面
测试成功!
2.定制注销成功页面
- 从上面的测试结果我们可以发现,注销之后页面被跳转到了spring security自动配置的页面,这显然不是我们想要的,我们想要的是注销之后跳转到我指定的页面,必须就跳转到首页
- 在看源码的时候,源码注释中有一个自定义注销的例子,在该例子的链式编程中,方法logoutSuccessUrl()就用于指定注销成功之后跳转的视图/请求的URL
- 照猫画虎实现注销成功跳转首页
http.logout().logoutSuccessUrl("/");//开启注销功能
- 测试
3.thymeleaf和spring security整合
- 但是,现在又有了新的需求了,当前系统的效果是不管你是什么角色用户,只要是进入首页,所有的资源信息的链接都能够查看到,只是不能请求到具体的资源进行查看,这显然不符合我们平时使用的习惯,应该是有哪些权限的用户就只能看到自己权限范围内的数据,而不能看到权限范围外的数据
- 解决:使用thymeleaf和spring security的整合实现,spring security提供用户的角色信息,thymeleaf在解析视图的时候按照角色决定某些信息模块是否显示
- 这就是使用thymeleaf的好处,它可以和许多东西进行整合
1.使用步骤
-
导入thymeleaf和spring security整合的依赖
<!--thymeleaf和spring security整合依赖,注意:直接导入版本5的依赖, 4太老了,2018就停更了,有些功能不能适配最新的spring security了--> <!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 --> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> <version>3.0.4.RELEASE</version> </dependency>
-
作用:可以在thymeleaf中写一些spring security的操作
-
首先改造登陆和注销按钮
-
首先导入命名空间
xmlns:sec="http://www.thymeleaf.org/extras/spring-security" //注意:直接写这个就行,不用写securityX来规定版本,规定之后反而没有提示了
2.解决登陆按钮+注销按钮显示问题
- 使用sec前缀接管节点属性,注意:我们引入的命名空间就已经说明了这个sec前缀就是spring security和thymeleaf的集合,所有它同时具有二者的特性:①th特性:接管标签属性 ②spring security特性:可以获取用户认证的信息,结合二者就可以通过spring security获取用户认证的信息+th判断来决定前端视图上某些部分是不是要显示
<!--登录注销--> <div class="right menu"> <!--未登录--> <div sec:authorize="!isAuthenticated()"> <a class="item" th:href="@{/toLogin}"> <i class="address card icon"></i> 登录 </a> </div> <div sec:authorize="isAuthenticated()" > 用户名:<span sec:authentication="name" style="display: inline"></span> <strong>|</strong> 角 色:<span sec:authentication="principal.authorities" style="display: inline"></span> <a class="item" th:href="@{/logout}" style="display: inline-block"> <i class="sign-out icon"></i> 注销 </a> </div> </div>
- 测试
3.解决动态菜单问题
- 要实现动态菜单的功能,显然我们需要进行条件判断,通过判断请求这个页面的用户的角色来判断某一个数据模块是否显示
- 测试
测试完成!
4.小结
- spring security的注销功能和用户未认证就请求资源时将其跳转登陆认证页面的功能一样,spring security已经帮我们实现好了,我们只需要去config类中调用对应的方法开启即可
- 我们除了直接使用spring security提供的默认注销配置之外,我们还可以实现个性化的配置,可以控制请求什么url触发spring security的注销功能,可以定义注销的时候销毁哪些数据,可以定义注销成功之后跳转的页面等
- 为了实现前端页面展示的数据和后端spring security认证的角色相互绑定,对应的角色只能查看对应的数据,对于角色权力之外的数据不可见的功能,我们可以将spring security和thymeleaf进行整合
- 首先需要导入整合的依赖,注意使用最新版本,修复了很多BUG
- 其次是在要使用的视图模板中导入命名空间,否则视图模板会报错,且写的时候不会给我们智能提示
- 最后就是使用二者整合之后的语法控制标签的行为,在什么情况下显示,什么情况下不显示