阻止重定向登录Spring Security

我有Spring MVC + Spring Security项目。

 ...  ...   

如果用户进入登录页面,如果成功将被重定向到“/ security / success”,我在控制器中使用会话对象执行更多操作(记录userID,…等)

我的问题是当一个GUEST用户要去/ dashboard / myaccount(需要AUTH)时,他被重定向到LOGIN页面(我不想要,我更喜欢抛出404)。 之后Spring Security没有重定向到/ security / success。 而是重定向到/ dashboard / myaccount。

在GUEST尝试访问AUTH页面的情况下,我更愿意找到一种方法来完全禁用此重定向到登录页面。

有办法做到这一点吗?

TNX

我们添加一个新的authenticationEntryPoint:

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

发现:always-use-default-target =“true”

我这样,我的控制器function总是在任何登录后调用。

在SpringSecurity 4的带注释配置中,您可以执行以下操作:

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

}