Prevent redirect to login for Spring Security

后端 未结 3 1129
甜味超标
甜味超标 2021-01-01 17:29

I have Spring MVC + Spring Security project.



        
相关标签:
3条回答
  • 2021-01-01 17:37

    We add a new authenticationEntryPoint:

    <http auto-config="true" access-denied-page="/security/accessDenied" use-expressions="true"
          disable-url-rewriting="true" entry-point-ref="authenticationEntryPoint"/>
    
    <beans:bean id="authenticationEntryPoint" class="a.b.c..AuthenticationEntryPoint">
        <beans:constructor-arg name="loginUrl" value="/security/login"/>
    </beans:bean>
    
    public class AuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {    
        public AuthenticationEntryPoint(String loginUrl)  {
            super(loginUrl);
        }
    
        @Override
        public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
            response.sendError(403, "Forbidden");
        }
    }
    
    0 讨论(0)
  • 2021-01-01 17:51

    In annotated configuration in SpringSecurity 4 you can do:

    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // ....
        http.exceptionHandling().authenticationEntryPoint(new AuthenticationEntryPoint() {
    
            @Override
            public void commence(HttpServletRequest request, HttpServletResponse response,
                    AuthenticationException authException) throws IOException, ServletException {
                if (authException != null) {
                    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
                    response.getWriter().print("Unauthorizated....");
                }
            }
        });
        // ....
    }
    

    }

    0 讨论(0)
  • 2021-01-01 17:55

    Found this: always-use-default-target="true"

    I this way, my controller function is always invoked after any login.

    0 讨论(0)
提交回复
热议问题