3 用户退出 3.1 需求分析
操作流程如下:
1、用户点击退出,弹出退出确认窗口,点击确定
用户退出要以下动作:
1、删除redis中的token 2、删除cookie中的token
3.2 API
认证服务对外提供退出接口。
[AppleScript] 纯文本查看 复制代码
?
1 | @ApiOperation ( "退出" ) public ResponseResult logout ( ) ; |
3.3 服务端
认证服务提供退出接口。 3.3.1 DAO
无。
3.3.2 Service
[AppleScript] 纯文本查看 复制代码
?
1 2 3 4 5 | / / 从redis中删除令牌 public boolean delToken ( String access_token ) { String name = "user_token:" + access_token; stringRedisTemplate. delete ( name ) ; return true ; } |
3.3.3 Controller
[AppleScript] 纯文本查看 复制代码
?
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 | / / 退出 @Override @PostMapping ( "/userlogout" ) public ResponseResult logout ( ) { / / 取出身份令牌 String uid = getTokenFormCookie ( ) ; / / 删除redis中token authService.delToken ( uid ) ; / / 清除cookie clearCookie ( uid ) ; return new ResponseResult ( CommonCode.SUCCESS ) ; } / / 清除cookie private void clearCookie ( String token ) { CookieUtil.addCookie ( response , cookieDomain , "/" , "uid" , token , 0 , false ) ; } |
3.3.4退出URL放行
认证服务默认都要校验用户的身份信息,这里需要将退出url放行。
在WebSecurityConfig类中重写 configure(WebSecurity web)方法,如下:
[AppleScript] 纯文本查看 复制代码
?
1 2 3 4 | @Override public void configure ( WebSecurity web ) throws Exception { web. ignoring ( ) .antMatchers ( "/userlogin" , "/userlogout" ) ; } |