Java 为Spring安全性防止重定向到登录
我有SpringMVC+SpringSecurity项目Java 为Spring安全性防止重定向到登录,java,spring,security,redirect,spring-mvc,Java,Spring,Security,Redirect,Spring Mvc,我有SpringMVC+SpringSecurity项目 <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', '
<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”,在那里我使用会话对象在控制器中执行更多操作(记录userID,…等等)
我的问题是,当一个来宾用户进入/dashboard/myaccount(需要身份验证)时,他被重定向到登录页面(我不想这样,我更喜欢抛出404)。此后,Spring安全性不再重定向到/Security/success。而是重定向到/dashboard/myaccount
我更愿意找到一种方法,在来宾试图访问身份验证页面时,完全禁用登录页面的重定向
有什么办法吗
Tnx发现:始终使用default target=“true”
这样,每次登录后都会调用我的控制器功能。我们添加了一个新的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");
}
}
公共类AuthenticationEntryPoint扩展了LoginRuthenAuthenticationEntryPoint{
公共身份验证入口点(字符串loginUrl){
超级(罗吉努尔);
}
@凌驾
public void start(HttpServletRequest请求、HttpServletResponse响应、AuthenticationException authException)引发IOException、ServletException{
响应。发送错误(403,“禁止”);
}
}
在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....");
}
}
});
// ....
}
}无需创建自己的身份验证入口点,请使用:org.springframework.security.web.authentication。Http403ForbiddenEntryPoint@unify我很高兴我能如此清晰地看到你的评论。请你看看我的问题好吗?太好了,以下简短的符号救了我:
.authenticationEntryPoint(HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
我发现这个建议在另一种情况下对我有帮助。谢谢