亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Spring Boot 2結合Spring security + JWT實現微信小程序登錄

瀏覽:111日期:2022-06-25 08:45:35

項目源碼:https://gitee.com/tanwubo/jwt-spring-security-demo

登錄

通過自定義的WxAppletAuthenticationFilter替換默認的UsernamePasswordAuthenticationFilter,在UsernamePasswordAuthenticationFilter中可任意定制自己的登錄方式。

用戶認證

需要結合JWT來實現用戶認證,第一步登錄成功后如何頒發token。

public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler { @Autowired private JwtTokenUtils jwtTokenUtils; @Override public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException { // 使用jwt管理,所以封裝用戶信息生成jwt響應給前端 String token = jwtTokenUtils.generateToken(((WxAppletAuthenticationToken)authentication).getOpenid()); Map<String, Object> result = Maps.newHashMap(); result.put(ConstantEnum.AUTHORIZATION.getValue(), token); httpServletResponse.setContentType(ContentType.JSON.toString()); httpServletResponse.getWriter().write(JSON.toJSONString(result)); }}

第二步,棄用spring security默認的session機制,通過token來管理用戶的登錄狀態。這里有倆段關鍵代碼。

@Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable().sessionManagement()// 不創建Session, 使用jwt來管理用戶的登錄狀態.sessionCreationPolicy(SessionCreationPolicy.STATELESS)......; }

第二步,添加token的認證過濾器。

public class JwtAuthenticationTokenFilter extends OncePerRequestFilter { @Autowired private AuthService authService; @Autowired private JwtTokenUtils jwtTokenUtils; @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { log.debug('processing authentication for [{}]', request.getRequestURI()); String token = request.getHeader(ConstantEnum.AUTHORIZATION.getValue()); String openid = null; if (token != null) { try {openid = jwtTokenUtils.getUsernameFromToken(token); } catch (IllegalArgumentException e) {log.error('an error occurred during getting username from token', e);throw new BasicException(ExceptionEnum.JWT_EXCEPTION.customMessage('an error occurred during getting username from token , token is [%s]', token)); } catch (ExpiredJwtException e) {log.warn('the token is expired and not valid anymore', e);throw new BasicException(ExceptionEnum.JWT_EXCEPTION.customMessage('the token is expired and not valid anymore, token is [%s]', token)); }catch (SignatureException e) {log.warn('JWT signature does not match locally computed signature', e);throw new BasicException(ExceptionEnum.JWT_EXCEPTION.customMessage('JWT signature does not match locally computed signature, token is [%s]', token)); } }else { log.warn('couldn’t find token string'); } if (openid != null && SecurityContextHolder.getContext().getAuthentication() == null) { log.debug('security context was null, so authorizing user'); Account account = authService.findAccount(openid); List<Permission> permissions = authService.acquirePermission(account.getAccountId()); List<SimpleGrantedAuthority> authorities = permissions.stream().map(permission -> new SimpleGrantedAuthority(permission.getPermission())).collect(Collectors.toList()); log.info('authorized user [{}], setting security context', openid); SecurityContextHolder.getContext().setAuthentication(new WxAppletAuthenticationToken(openid, authorities)); } filterChain.doFilter(request, response); }}接口鑒權

第一步,開啟注解@EnableGlobalMethodSecurity。

@SpringBootApplication@EnableGlobalMethodSecurity(prePostEnabled = true)public class JwtSpringSecurityDemoApplication { public static void main(String[] args) { SpringApplication.run(JwtSpringSecurityDemoApplication.class, args); }}

第二部,在需要鑒權的接口上添加@PreAuthorize注解。

@RestController@RequestMapping('/test')public class TestController { @GetMapping @PreAuthorize('hasAuthority(’user:test’)') public String test(){ return 'test success'; } @GetMapping('/authority') @PreAuthorize('hasAuthority(’admin:test’)') public String authority(){ return 'test authority success'; }}

到此這篇關于Spring Boot 2結合Spring security + JWT實現微信小程序登錄的文章就介紹到這了,更多相關Spring Boot Spring security JWT微信小程序登錄內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: 微信
相關文章:
主站蜘蛛池模板: 成人久久18免费网站入口 | 强开小嫩苞一区二区三区l 婷婷丁香色综合狠狠色 | 精品国产一区二区二三区在线观看 | 性生活免费大片 | 成人毛片18女人毛片 | 麻豆视频成人 | 亚洲精品久久久久久中文字幕小说 | 你懂的免费在线视频 | 亚洲综合伊人制服丝袜美腿 | 美国美女黄色片 | 国产白嫩在线观看视频 | 一级特级欧美aa毛片免费 | 日本的黄色录像 | 国产伦精品一区二区三区视频金莲 | 日韩亚州 | 国产乱子精品免费视观看片 | 香蕉视频在线观看网址 | 色一色在线观看视频网站 | 激情五月色综合亚洲小说 | 亚洲美女一级毛片 | 一区二区三区免费在线观看 | 激情爱爱网站 | 欲色影视天天一区二区三区色香欲 | 最新国产你懂的在线网址 | 色婷婷一区二区三区四区成人 | 久久天堂成人影院 | 又大又爽又湿又紧a视频 | 日韩一二区| 国产美女在线精品亚洲二区 | 成人激情视频在线观看 | 在线免费观看小视频 | 欧美日韩不卡视频一区二区三区 | 欧美亚洲日本国产 | 日韩免费观看一级毛片看看 | 毛片网子| 国产精品视频一区日韩丝袜 | 网红主播大秀一区二区 | 欧美伦理片在线 | 婷婷sese| 亚洲午夜一级毛片 | 亚洲精品aⅴ一区二区三区 亚洲精品aⅴ中文字幕乱码 |