安全认证

在使用HTTP Basic 认证期间,开发者并不能够自定义收集用户凭证的对话框。基于HTTP的表单(FORM)认证也是JAVA EE 容器厂商必须支持的一种认证机制之一。这一认证机制采用普通的Web页面收集用户凭证信息,因此它在用户友好性方面广受亲睐。为此,IMS系统采用基于Spring Security的表单认证。
AuthenticationProcessingFilter和AuthenticationProcessingFilterEntryPoint是用于表单认证的过滤器和辅助类。其中,处于过滤链中的AuthenticationProcessingFilter用于拦截Web请求, 并从HttpServletRequest请求中抽取出用户名、密码等用户凭证信息。与此同时,AuthenticationProcessingFilterEntryPoint用于实施表单认证。
接下来,我们通过对Spring Security扩展和配置以支持表单认证。
n 首先,在web.xml描述符中配置如下信息,该过滤器从Spring容器中查找名为“springSecurityFilterChain”的Filter。该过滤器内部构造了VirtualFilterChain以支持多个Spring Security 提供的安全Filter,这样就无须在Web.xml中配置每个Filter。
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name> springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
n 其次,在Spring配置文件中配置与1对应的Filter,该过滤器是IMS系统安全配置中心。其中,auto-config=”true”相当于是表单认证,匿名认证,Basic认证,注销服务,Remember认证集合。其中sec:form-login是对表单认证的重写,sec:logout是对注销服务的重写。针对配置做如下详细描述
entry-point-ref="authenticationProcessingFilterEntryPoint",一般执行FilterSecurityInterceptor时,认证失败或者匿名用户授权失败后的重定向。其中loginFromUrl指定授权失败后的页面地址;forceHttps指定当serverSideRedirect=true时,将Http请求转化为HTTPS;serverSideRedirect指定是否服务器端转发。
<bean id="authenticationProcessingFilterEntryPoint"class="org.springframework.security.ui.webapp.AuthenticationProcessingFilterEntryPoint">
<property name="loginFormUrl" value="/login.jsp" />
<property name="forceHttps" value="false" />
<property name="serverSideRedirect" value="true" />
</bean>
lowercase-comparisons指定是否将请求URL转成小写与Pattern匹配
access-denied-page指定非匿名用户授权失败后的地址
sec:intercept-url指定模式与权限对应关系,而且sec:intercept-url有顺序关系,当sec:intercept-url匹配成功后,会短路不再执行之后的sec:intercept-url,所以,一般特定的URL放在最前面,如果配置多个sec:intercept-url,千万不要把pattern=”/**”放在最前面,否则后面的配置没意义
pattern URL匹配路径
  path-type="ant"匹配模式,有Ant 和Regex,默认为Ant
form-login为表单认证配置,其中always-use-default-target="true"指定HttpSession中SPRING_SECURITY_SAVED_REQUEST_KEY的请求不存在时,重定向到”/”。/login.jsp指定实施表单认证的登录页面,login-processing-url="/j_spring_security_check"指定当请求为/j_spring_security_check时,才实施表单认证。当认证失败后,跳转到authentication-failure-url指定页
logout为注销配置,其中logout-success-url指定注销后的跳转页(注销不会失败),logout-url指定当请求为"/j_spring_security_logout”才实施注销服务。而invalidate-session指定注销时使session中的信息无效
<sec:http auto-config="true"
entry-point-ref="authenticationProcessingFilterEntryPoint" path-type="ant"
lowercase-comparisons="false" access-denied-page="/accessDenied.jsp">
<sec:intercept-url pattern="/login.jsp*" filters="none" />
<sec:intercept-url pattern="/captchaImage*" filters="none" />
<sec:intercept-url pattern="/**" access="ROLE_USER" />
<sec:form-login always-use-default-target="true"
login-page="/login.jsp" login-processing-url="/j_spring_security_check"
authentication-failure-url="/login.jsp?login_error=1" />
<sec:logout logout-success-url="/login.jsp" logout-url="/j_spring_security_logout"
invalidate-session="true" />
</sec:http>
n 下面对IMS系统登录界面的安全相关设置。需要输出验证失败相关信息,param.login_error指定表单认证中的authentication-failure-url属性,当认证失败时,输出SPRING_SECURITY_LAST_EXCEPTION.message信息,SPRING_SECURITY_LAST_EXCEPTION指定表示认证失败信息的异常信息。
<c:if test="${not empty param.login_error}">
      <font color="red">
        Your login attempt was not successful, try again.<br/><br/>
        Reason: <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}"/>.
      </font>
    </c:if>
n 再看form表单设置 。action必须为/'j_spring_security_check,这样才能实施表单认证,j_username和j_password是表单认证中默认的字段名,表单过滤器会从请求中抽取;我们还使用了Remember认证,注意checkbox名称必须为_spring_security_remember_me,我们还集成了验证码,输入框的名字为j_captcha_response,图片请求必须为captchaImage.htm。至此,IMS基于Spring Security设置结束
<form name="f" action="<c:url value='j_spring_security_check'/>" method="POST">
      <table>
        <tr><td>User:</td><td><input type='text' name='j_username' value='<c:if test="${not empty param.login_error}"><c:out value="${SPRING_SECURITY_LAST_USERNAME}"/></c:if>'/></td></tr>
        <tr><td>Password:</td><td><input type='password' name='j_password'></td></tr>
        <tr><td><input type="checkbox" name="_spring_security_remember_me"></td><td>Don't ask for my password for two weeks</td></tr>
        <tr><td>请将图片中显示的内容填充到输入框中<input name="j_captcha_response"/></td><td><img src="captchaImage.htm" onclick=""/></td></tr>
        <tr><td colspan='2'><input name="submit" type="submit"></td></tr>
        <tr><td colspan='2'><input name="reset" type="reset"></td></tr>
      </table>
    </form>

猜你喜欢

转载自tmmh.iteye.com/blog/1817625