国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Spring Boot 2結(jié)合Spring security + JWT實(shí)現(xiàn)微信小程序登錄

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

項(xiàng)目源碼:https://gitee.com/tanwubo/jwt-spring-security-demo

登錄

通過(guò)自定義的WxAppletAuthenticationFilter替換默認(rèn)的UsernamePasswordAuthenticationFilter,在UsernamePasswordAuthenticationFilter中可任意定制自己的登錄方式。

用戶(hù)認(rèn)證

需要結(jié)合JWT來(lái)實(shí)現(xiàn)用戶(hù)認(rèn)證,第一步登錄成功后如何頒發(fā)token。

public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler { @Autowired private JwtTokenUtils jwtTokenUtils; @Override public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException { // 使用jwt管理,所以封裝用戶(hù)信息生成jwt響應(yīng)給前端 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默認(rèn)的session機(jī)制,通過(guò)token來(lái)管理用戶(hù)的登錄狀態(tài)。這里有倆段關(guān)鍵代碼。

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

第二步,添加token的認(rèn)證過(guò)濾器。

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); }}接口鑒權(quán)

第一步,開(kāi)啟注解@EnableGlobalMethodSecurity。

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

第二部,在需要鑒權(quán)的接口上添加@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'; }}

到此這篇關(guān)于Spring Boot 2結(jié)合Spring security + JWT實(shí)現(xiàn)微信小程序登錄的文章就介紹到這了,更多相關(guān)Spring Boot Spring security JWT微信小程序登錄內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: 微信
相關(guān)文章:
主站蜘蛛池模板: 欧美亚洲国产视频 | 国产成人精视频在线观看免费 | 日一区二区 | 99视频免费在线观看 | 成人在线视频免费看 | 97dyy影院理论片 | 久草手机视频在线观看 | 男人的天堂中文字幕 | 国产午夜免费福利红片 | 香港三级日本三级人妇网站 | 午夜伊人| 国产欧美成人免费观看 | 91精品国产综合久久久久久 | 99re国产视频 | 99re这里只有精品99 | 国产美女一区二区三区 | 精产网红自拍在线 | 久久91精品牛牛 | 亚洲精品人成网在线播放影院 | 久久久久亚洲香蕉网 | 日韩特级毛片免费观看视频 | 欧美成人免费观看 | 欧美私人网站 | 成人免费视频网站 | 日韩不卡在线观看 | 精品久久久中文字幕一区 | 欧美一级一极性活片免费观看 | 国产综合久久 | 国产精品免费看 | 孩交啪啪网址 | 国产精品专区第二 | 国产在线视频h | 亚洲免费视频观看 | 日韩在线1 | 成人免费看黄网址 | 亚洲精品区在线播放一区二区 | 国产91专区| 欧美精品久久久久久久久大尺度 | 国产成人区 | 免费被黄网站在观看 | 久久综合一区二区三区 |