Filters:
ExceptionTranslationFilter:有两种异常抛出!
private void handleException(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
RuntimeException exception) throws IOException, ServletException {
if (exception instanceof AuthenticationException) {//如果是AuthenticationException的类型,直接发送到 sendStartAuthentication
if (logger.isDebugEnabled()) {
logger.debug("Authentication exception occurred; redirecting to authentication entry point", exception);
}
sendStartAuthentication(request, response, chain, (AuthenticationException) exception);
}
else if (exception instanceof AccessDeniedException) {
if (authenticationTrustResolver.isAnonymous(SecurityContextHolder.getContext().getAuthentication())) {
if (logger.isDebugEnabled()) {
logger.debug("Access is denied (user is anonymous); redirecting to authentication entry point",
exception);
}
sendStartAuthentication(request, response, chain, new InsufficientAuthenticationException(
"Full authentication is required to access this resource"));
}
else {
if (logger.isDebugEnabled()) {
logger.debug("Access is denied (user is not anonymous); delegating to AccessDeniedHandler",
exception);
}
accessDeniedHandler.handle(request, response, (AccessDeniedException) exception);
}
}
}
第一种:就是普通的授权异常了!AuthenticationException
1. 如果没有授权的访问,抛出 AuthenticationException的子类,判断它的类型为AuthenticationException之后,调用sendStartAuthentication方法去判断
protected void sendStartAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
AuthenticationException reason) throws ServletException, IOException {
// SEC-112: Clear the SecurityContextHolder's Authentication, as the
// existing Authentication is no longer considered valid
SecurityContextHolder.getContext().setAuthentication(null);
requestCache.saveRequest(request, response);
logger.debug("Calling Authentication entry point.");
authenticationEntryPoint.commence(request, response, reason);
}
清空SecurityContextHolder上下文中的Authentication , 然后委托给authenticationEntryPoint去发送分发页面!默认的是
LoginUrlAuthenticationEntryPoint(我们可在applicationContext.xml配置中,设置entry-point-ref="loginUrlAuthenticationEntryPoint",
这里的loginUrlAuthenticationEntryPoint是自定义的实现类)
<bean:bean id="loginUrlAuthenticationEntryPoint"
class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"
p:loginFormUrl="/login/welcome"> <!-- 这里默认是DefaultLoginPageGeneratingFilter.DEFAULT_LOGIN_PAGE_URL? --> /spring_security_login是默认发送到的登录界面 -->
</bean:bean>
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
String redirectUrl = null;
if (useForward) {//是否选择 Forward模式,如果不是,发送302重定向,跳转到 登录页面。默认为false.
if (forceHttps && "http".equals(request.getScheme())) {
redirectUrl = buildHttpsRedirectUrlForRequest(httpRequest);
}
if (redirectUrl == null) {
String loginForm = determineUrlToUseForThisRequest(httpRequest, httpResponse, authException);//提供给子类覆盖!,是否临时修改登录界面的URL.
if (logger.isDebugEnabled()) {
logger.debug("Server side forward to: " + loginForm);
}
RequestDispatcher dispatcher = httpRequest.getRequestDispatcher(loginForm);
dispatcher.forward(request, response);
return;
}
} else {
// redirect to login page. Use https if forceHttps true
redirectUrl = buildRedirectUrlToLoginPage(httpRequest, httpResponse, authException);//创建重定向URL
}
httpResponse.sendRedirect(httpResponse.encodeRedirectURL(redirectUrl));
}
第二种:是访问资源被Denied的异常,AccessDeniedException
分享到:
相关推荐
早一段时间学习了springsecurity3.0 框架,在开始阶段不知道导入那些必需jar包,经过摸索,总结出来最精简的jar包
spring security3.0权限控制方面的文档
SpringSecurity3.0相对比较稳定。本实例包含SpringSecurity3.0的基本配置,包含所需的Jar包和mysql数据库文件,直接导入myeclipes中并导入数据库即可运行,配置文件简单易懂,适合SpringSecurity初学者配置入门。...
教你使用 SpringSecurity 3.0 一步一步教你使用SpringSecurity,从保护web应用到保护业务方法调用
SpringSecurity3.0 教程 价值不错哦!
spring-security-web-3.0 spring-security-taglibs-3.0 spring-security-openid-3.0 spring-security-core-3.0 spring-security-config-3.0 spring-security-aspects-3.0 spring-security-acl-3.0
SpringSecurity3.0实现
Spring Security 3.0 权限管理精通,该系统是spring提供的企业级权限系统解决方案,因其优越的特性被广泛应用
英文版本,主要对spring security 3.0.x 进行了介绍, 以及如何使用spring 的 安全架构来进行 web 开发.
超级详细的spring security3.0教程,包含spring security安全框架的历史和来源,一步一步的教你如何实现项目的安全实现
SpringSecurity3.0.x官方参考文档(英文+中文双解版)
spring security3.0所有最新开发包及源码及文档 欢迎大家下载,祝大家学习愉快!。
Spring Security 的前身是 Acegi Security ,是 Spring 项目组中用来提供安全认证服务的框架。
简单springsecurity3.0的例子 做了详细注释,另外集成了tiles 和conversion插件,希望对你有帮助,里面有不对的地方请给我留言,我加你QQ一起讨论..注:我是通过maven管理的,如果你不是的话可能无法运行起来,只能看代码
Spring Security 3.0 安全权限管理手册(最新),可以作为公司权限管理资料用,权限管理将是系统中的非常重要的一个模块,权限的设计也是参考相关资料进行整理和补充。系统将通过数据库进行管理用户权限
Spring-Security3.0自定义表结构
介绍一个基于Spring Boot 3.0、Spring Cloud 2022 & Alibaba的微服务RBAC权限管理系统。该系统可以实现微服务RBAC权限管理,通过RBAC权限管理机制对用户访问系统的权限进行限制,从而提高系统的安全性和可用性。同时...
一步一步教你使用spring Security3
让你更好理解spring security开发流程,更好应用spring security!!
代码自己看,eclipse 环境最好用JDK7,JDK5会出问题