18

我有 Spring MVC + 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"/>

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

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

我希望找到一种方法来完全禁用此重定向到登录页面,以防 GUEST 尝试访问 AUTH 页面。

有什么办法吗?

肿瘤坏死因子

4

3 回答 3

14

我们添加一个新的 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");
    }
}
于 2013-04-08T09:25:58.140 回答
10

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

}

于 2015-06-09T11:07:51.200 回答
5

发现这个:总是使用默认目标=“真”

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

于 2013-04-06T06:49:58.263 回答