1.如果事想一次性访问直接在psotman,把Authorization添加上去,还有cookie,前端页面按发f12,添加上去就行
2.如果想直接跳出认证,需要修改后端framework模块下的自己添加自己的模块标记地方是我自己添加的。
还需要在下面security模块找到filter下的JwtAuthenticationTokenFilter,把这段代码复制替换掉相关代码即可
@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
@Autowired
private TokenService tokenService;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws ServletException, IOException {
String token = getTokenFromRequest(request);
if (token != null) {
// 只在有 Token 时进行认证
LoginUser loginUser = tokenService.getLoginUser(request);
if (loginUser != null) {
tokenService.verifyToken(loginUser);
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
loginUser, null, loginUser.getAuthorities());
authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
}
}
chain.doFilter(request, response);
}
private String getTokenFromRequest(HttpServletRequest request) {
String header = request.getHeader("Authorization");
if (header != null && header.startsWith("Bearer ")) {
return header.substring(7);
}
return null;
}
}