All Downloads are FREE. Search and download functionalities are using the official Maven repository.

templates.service.TokenAuthenticationManager.ftl Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
package ${packageName}.service.security;

import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.security.authentication.ReactiveAuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import ${packageName}.common.context.InnerContextHolder;
import ${packageName}.common.enums.InnerEnums;
import ${packageName}.common.utils.JwtTokenUtil;
import ${packageName}.common.vo.LoginUserVo;
import org.yes.tools.core.exception.YesBaseException;
import reactor.core.publisher.Mono;

/**
 * 自定义的登录验证器
* 由于SpringGateWay基于WebFlux,所以SpringSecruity很多原有写法,都得改为WebFlux的方式才能生效! */ @Component @Primary public class TokenAuthenticationManager implements ReactiveAuthenticationManager { @Autowired private UserServiceImpl userService; <#if isSpringCloud==1> @Override @SuppressWarnings("unchecked") public Mono authenticate(Authentication authentication) { InnerEnums innerType = InnerContextHolder.getInnerType(); //token String token = (String) authentication.getPrincipal(); if (innerType.equals(InnerEnums.ALL_PASS)) { SecurityContextHolder.getContext().setAuthentication(authentication); return Mono.just(authentication); } else if (innerType.equals(InnerEnums.HALF_PASS)) { if (token == null || "null".equals(token)) { SecurityContextHolder.getContext().setAuthentication(authentication); return Mono.just(authentication); } return getToken(token); } else { return getToken(token); } } <#else > @Override @SuppressWarnings("unchecked") public Mono authenticate(Authentication authentication) { InnerEnums innerType = InnerContextHolder.getInnerType(); //token String token = (String) authentication.getPrincipal(); if (innerType.equals(InnerEnums.ALL_PASS) || innerType.equals(InnerEnums.HALF_PASS)) { SecurityContextHolder.getContext().setAuthentication(authentication); return Mono.just(authentication); } else { return getToken(token); } } private Mono getToken(String token) { Claims claims = null; try { claims = JwtTokenUtil.parseJwtRsa256(token); } catch (Exception e) { return Mono.error(new YesBaseException("401", "token验证失败,请重新登录")); } LoginUserVo userDetails = (LoginUserVo) userService.loadUserByUsername(claims.getSubject()); userDetails.setToken(token); Authentication authen = new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities()); SecurityContextHolder.getContext().setAuthentication(authen); return Mono.just(authen); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy