SpringSecurity使用技巧

1、鉴权处理页通常包括四个方面的设定,分别是鉴权失败、鉴权成功、未鉴权访问、已鉴权但访问了受保护权限。如何自

 

定义这四类处理。

 

鉴权失败的默认处理页面是"/spring_security_login?login_error",其默认处理类为SimpleUrlAuthenticationFailureHandler。 

 

鉴权成功的默认处理页面是"/",其默认处理类为SimpleUrlAuthenticationSuccessHandler。 

 

自定义配置如下:

 

 <beans:bean id="authenticationProcessingFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationProcessingFilter">
  <beans:property name="authenticationManager" ref="authenticationManager"/>
        <beans:property name="authenticationFailureHandler">
            <beans:bean class="example.MyAuthenticationFailureHandler">
                <beans:property name="defaultFailureUrl" value="/pages/Login/login.do?error=true"/>
            </beans:bean>
        </beans:property>
        <beans:property name="authenticationSuccessHandler">
            <beans:bean class="example.MyAuthenticationSuccessHandler">
                <beans:property name="defaultTargetUrl" value="/"/>
            </beans:bean>
        </beans:property>
    </beans:bean>

另外在<http>中增加配置如下:

 

<custom-filter before="AUTHENTICATION_PROCESSING_FILTER" ref="authenticationProcessingFilter"/>

 

其中的example.MyAuthenticationFailureHandler和example.MyAuthenticationSuccessHandler为自定义的失败与成功处理类,源码如下:

 

public class MyAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler{
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
            AuthenticationException exception) throws IOException, ServletException {
        //增加自己的处理逻辑
        super.onAuthenticationFailure(request, response, exception);
    }
}

 

public class MyAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler{
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
            Authentication auth) throws IOException, ServletException {
        //增加自己的处理逻辑       

        super.onAuthenticationSuccess(request, response, auth);
    }
}

 

未鉴权访问、已鉴权但访问了受保护权限的自定义配置如下:

 

 <beans:bean id="authenticationEntryPoint"
  class="example.MyAuthenticationEntryPoint">
  <beans:property name="loginFormUrl" value="/login.htm" />
 </beans:bean>

 <beans:bean id="accessDeniedHandler"
  class="example.MyAccessDeniedHandler">
  <beans:property name="errorPage" value="/accessDenied.htm" />
 </beans:bean>

另外在<http>中增加配置如下:

 

<http entry-point-ref="authenticationEntryPoint">

    <access-denied-handler ref="accessDeniedHandler"/>

</http>

 

其中的example.MyAuthenticationEntryPoint和example.MyAccessDeniedHandler源码参考如下:

 

public class MyAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
            throws IOException, ServletException {

        //增加自己的处理逻辑

        super.commence(request, response, authException);
    }
}

 

public class MyAccessDeniedHandler extends AccessDeniedHandlerImpl{
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException)
    throws IOException, ServletException {
        //增加自己的处理逻辑
        super.handle(request, response, accessDeniedException);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值