2013-04-06 107 views
12

我有Spring MVC + Spring Security項目。防止重定向到Spring Security登錄

<http auto-config="true" access-denied-page="/security/accessDenied" use-expressions="true" disable-url-rewriting="true"> 

... 
<intercept-url pattern="/dashboard/myaccount/**" access="hasAnyRole('ROLE_PERSON', 'ROLE_DEALER')"/> 
... 

<form-login login-page="/security/login" authentication-failure-url="/security/login?error=true" 
       default-target-url="/security/success" username-parameter="email" 
       password-parameter="secret"/> 
<logout invalidate-session="true" logout-success-url="/index" logout-url="/security/logout"/> 

如果用戶進入登錄頁面,如果成功的話將被重定向到「/安全/成功」在那裏我與會話對象控制器做更多的東西(記錄用戶ID,...等)

我的問題是,當一個GUEST用戶要去/儀表板/ myaccount(這需要AUTH),他被重定向到LOGIN頁面(我不想,我更喜歡404拋出)。之後,Spring Security沒有重定向到/安全/成功。而是重定向到/ dashboard/myaccount。

我希望找到一種方法來在GUEST嘗試訪問AUTH頁面的情況下完全禁用此重定向到登錄頁面。

有沒有辦法做到這一點?

TNX

回答

5

發現這一點:總是使用默認目標=「真」

我這樣,我的控制器功能總是被任何登錄後調用。在SpringSecurity 4

<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"); 
    } 
} 
11

我們添加一個新的AuthenticationEntryPoint

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...."); 
      } 
     } 
    }); 
    // .... 
} 

}

+1

無需創建自己的AuthenticationEntryPoint,使用:組織。 springframework.security.web.authentication.Http403ForbiddenEntryPoint – unify 2014-08-17 22:28:05

+0

@unify我很高興我看着你的評論非常清潔 – ndrone 2016-09-14 17:25:34

+0

你會pl輕鬆看看我的問題太https://stackoverflow.com/q/47849246/2556354 – madz 2017-12-17 15:54:42

2

在標註的配置,你可以這樣做::

+0

我發現這個建議幫助我在另一種情況。謝謝 – madz 2017-12-18 02:05:09

相關問題