Need to configure authenticationEntryPoint WebSecurityConfigurerAdapter. If you do not specify a custom authenticationEntryPoint, call LoginUrlAuthenticationEntryPoint by default.

Public class SecurityConfiguration extends WebSecurityConfigurerAdapter {/ / custom authentication validation, Judge request or common interface @ Bean public MyAuthenticationEntryPoint MyAuthenticationEntryPoint () {return new MyAuthenticationEntryPoint("/login"); } @Override protected void configure(HttpSecurity http) throws Exception { ... Http.formlogin () // Turn off cross-site request protection... Exceptionhandler.exceptionhandling () // Customize exceptionHandler.accessdeniedHandler (myAccessDeniedHandler) // Customize exceptionHandler.exceptionHandling () // Customize ExceptionHandler . AuthenticationEntryPoint (myAuthenticationEntryPoint) / / add JWT filter validation. JWT and () .addFilterBefore(jwtAuthenticationTokenFilter(), UsernamePasswordAuthenticationFilter.class); . }}Copy the code

Inheritance LoginUrlAuthenticationEntryPoint, modify commence interface, judge the request URI. If the OAuth interface is used, the login interface is displayed. If the OAuth interface is used, 401 is returned.

public class MyAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint { /** * @param loginFormUrl URL where the login page can be found. Should either be * relative to the web-app context path (include a leading {@code /}) or an  absolute * URL. */ public MyAuthenticationEntryPoint(String loginFormUrl) { super(loginFormUrl); } @Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { System.out.println(request.getRequestURI()); if (request.getRequestURI().contains("/oauth/")) { super.commence(request, response, authException); } else { response.setCharacterEncoding("UTF-8"); response.setContentType("application/json"); / / return JSONResponse response. GetWriter () println (JSONUtil. Parse (JSONResponse. Unauthorized (" no Token or Token failure! Please log in again!" ))); response.getWriter().flush(); }}Copy the code