2015-09-04 89 views

回答

0

我由http节点下添加以下内容到我的春季安全XML解决了这个问题:

<security:access-denied-handler error-page="/#/login" /> 
<security:session-management invalid-session-url="/#/login" session-fixation-protection="changeSessionId" /> 
0

您可以指定你的应用程序通过web.xml中的错误页面元素将其指定

如处理异常或HTTP状态代码:web.xml中

<error-page> 
    <error-code>401</error-code> 
    <location>/login.html</location> 
</error-page> 

相同的方式,处理其他HTTP状态码404代表页面未找到页面。

+2

有没有办法使用基于java/annotaion的配置来做到这一点? –

0

对于spring-security应用基于spring-boot

定义处理豆:

@Component 
public class CommenceEntryPoint implements AuthenticationEntryPoint, Serializable { 
    private static final long serialVersionUID = 565662170056829238L; 

    // invoked when user tries to access a secured REST resource without supplying any credentials, 
    @Override 
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException { 
     // send a json object, with http code 401, 
     // response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized"); 

     // redirect to login page, for non-ajax request, 
     response.sendRedirect("/login.html"); 
    } 
} 

在安全配置类(例如WebSecurityConfig):

的Autowire豆:

@Autowired 
private CommenceEntryPoint unauthorizedHandler; // handle unauthorized request, 

指定的处理程序:

.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and() // excepion handler, 

提示:

  • 这仅适用于非Ajax请求,
  • 对于Ajax请求,更好的回报401代码,并让前端处理。
    如果要使用401响应,请在CommenceEntryPoint.commence()中使用response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");而不是response.sendRedirect()