认证、授权攻略三(2)、spring security登录form-login

上一篇:【认证、授权攻略三(1)、spring security(与springmvc集成实战)】

form-login元素介绍:
http 元素下的 form-login 元素是用来定义表单登录信息的。当我们什么属性都不指定的时候 Spring Security会为我们生成一个默认的登录页面。如果不想使用默认的登录页面,我们可以指定自己的登录页面。

自定义登录页面
spring-security.xml



	
	
	
	
	
	
	
		
     	
     	
     	
     	
     	
      	
      	
      	
      	
      	
     	
     	
     				
     	
     	
   	 
   	
   	
   	
      	
         	
         		
            	
            	
         	
      	
   	

说明:
login-page:自定义登录页面;
username-parameter:表示登录时用户名使用的是哪个参数,默认是 “username”。
password-parameter:表示登录时密码使用的是哪个参数,默认是 “password”。
login-processing-url:表示登录时提交的地址,默认是 “/login”。这个只是 Spring Security 用来标记登录页面使用的提交地址,真正关于登录这个请求是不需要用户自己处理的。

放行login.jsp
我们之前配置的是所有的请求都需要 ROLE_USER 权限,这意味着我们自定义的 “/login.jsp” 也需要该权限,这样就会形成一个死循环了。解决办法是我们需要给 “/login.jsp” 放行。解决方法如下:
1、指定ANONYMOUS匿名用户可以访问,无需登录;

2、指定安全性为none,不走权限控制的过滤器链

它们两者的区别是(1)将进入 Spring Security 定义的一系列用于安全控制的 filter,而后者(2)不会。当指定一个 http 元素的 security 属性为 none 时,表示其对应 pattern 的 filter 链为空。从 3.1 开始,Spring Security 允许我们定义多个 http 元素以满足针对不同的 pattern 请求使用不能的 filter 链。当为指定 pattern 属性时表示对应的 http 元素定义将对所有的请求发生作用。

指定登录成功后的页面
default-target-url:
默认情况下,我们在登录成功后会返回到原本受限制的页面。但如果用户是直接请求登录页面,登录成功后应该跳转到哪里呢?默认情况下它会跳转到当前应用的根路径,即欢迎页面。通过指定 form-login 元素的 default-target-url 属性,我们可以让用户在直接登录后跳转到指定的页面。如果想让用户不管是直接请求登录页面,还是通过 Spring Security 引导过来的,登录之后都跳转到指定的页面,我们可以通过指定 form-login 元素的 always-use-default-target 属性为 true 来达到这一效果。

authentication-success-handler-ref:
authentication-success-handler-ref 对应一个 AuthencticationSuccessHandler 实现类的引用。如果指定了 authentication-success-handler-ref,登录认证成功后会调用指定 AuthenticationSuccessHandler 的 onAuthenticationSuccess 方法。我们需要在该方法体内对认证成功做一个处理,然后返回对应的认证成功页面。使用了 authentication-success-handler-ref 之后认证成功后的处理就由指定的 AuthenticationSuccessHandler 来处理,之前的那些 default-target-url 之类的就都不起作用了。
AuthencticationSuccessHandler 实现类:

package com.javasgj.ssm.login.handler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

@Component
public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {

	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
			Authentication authentication) throws IOException, ServletException {
		
		response.sendRedirect(request.getContextPath() + "/index1.jsp");
	}
}

指定登录失败后的页面
authentication-failure-url:
默认情况下登录失败后会返回登录页面,我们也可以通过 form-login 元素的 authentication-failure-url 来指定登录失败后的页面。需要注意的是登录失败后的页面跟登录页面一样也是需要配置成在未登录的情况下可以访问,否则登录失败后请求失败页面时又会被 Spring Security 重定向到登录页面。

authentication-failure-handler-ref:
类似于 authentication-success-handler-ref,authentication-failure-handler-ref 对应一个用于处理认证失败的 AuthenticationFailureHandler 实现类。指定了该属性,Spring Security 在认证失败后会调用指定 AuthenticationFailureHandler 的 onAuthenticationFailure 方法对认证失败进行处理,此时 authentication-failure-url 属性将不再发生作用。
AuthenticationFailureHandler 实现类:

package com.javasgj.ssm.login.handler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

@Component
public class AuthenticationFailureHandlerImpl implements AuthenticationFailureHandler {

	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException exception) throws IOException, ServletException {

		response.sendRedirect(request.getContextPath() + "/error1.jsp");
	}
}

http-basic:弹窗进行认证

认证、授权攻略三(2)、spring security登录form-login_第1张图片
需要注意的是当我们同时定义了 http-basic 和 form-login 元素时,form-login 将具有更高的优先级。即在需要认证的时候 Spring Security 将引导我们到登录页面,而不是弹出一个窗口。

你可能感兴趣的:(认证,授权攻略)