色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

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

瀏覽:146日期: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微信小程序登錄內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: 微信
相關文章:
主站蜘蛛池模板: 欧美精品亚洲一区二区在线播放 | 国产日韩久久久久69影院 | 欧美一级大尺度毛片 | 午夜剧场成年 | 99精品久久精品一区二区 | 涩涩网站 | 日韩美女一级视频 | 美女黄色在线看 | 欧美久色 | 久久99国产亚洲高清观看首页 | 久久高清一级毛片 | 亚洲国产欧美在线人成 | 中文字幕s级优女区 | 欧美片能看的一级毛片 | 美女张腿男人桶免费视频 | 国产精品久久久久影视不卡 | 中文字幕久久亚洲一区 | 成人18免费视频 | 色综合久久久久久888 | 精品国产欧美另类一区 | 国产精品美女一区二区三区 | 成人综合在线观看 | 99久久精品免费看国产一区二区三区 | 97在线观看免费视频 | 久久久久欧美精品网站 | 99在线观看精品视频 | 毛片精品| 高清国产美女一级a毛片录 高清国产亚洲va精品 | 国产日韩精品一区在线观看播放 | 免费毛片儿 | 精品国产九九 | 日韩一区二区三区视频在线观看 | 国产精品久久精品 | 香蕉国产人午夜视频在线 | 老司机亚洲精品影院在线 | 一区二区精品在线观看 | 亚洲一区二区三区四区在线 | 国产成人爱片免费观看视频 | 欧美成人h精品网站 | 欧美午夜不卡在线观看最新 | 日韩高清成人毛片不卡 |