zoukankan      html  css  js  c++  java
  • 【学习、总结】Spring security 登陆超时处理

    目标:

          用户登陆超过一定时间,在页面做请求时,提示类似登陆已超时,请重新登陆信息。

    实现:

    1.超时时间配置(web.xml):

    <session-config>
         <!-- 10分钟 --> 
    <session-timeout>10</session-timeout> <tracking-mode>COOKIE</tracking-mode> <cookie-config> <secure>false</secure> <http-only>true</http-only> </cookie-config>
    </session-config>

    2.session超时过滤、ajax请求处理(spring-security.xml)

       <!-- invalidSession.htm 是一个不存在的页面 -->
       <session-management invalid-session-url="/invalidSession.htm"
                session-fixation-protection="newSession">
                <concurrency-control session-registry-ref="sessionRegistry"
                    max-sessions="1" error-if-maximum-exceeded="true" />
        </session-management>
        
        <beans:bean id="sessionRegistry"
            class="org.springframework.security.core.session.SessionRegistryImpl" />
            
        <!-- 未登录的切入点 -->
        <beans:bean id="authenticationProcessingFilterEntryPoint"
            class="com.xxx.xxx.web.controller.auth.MyLoginUrlAuthenticationEntryPoint">
            <beans:property name="loginFormUrl" value="/login.htm"></beans:property>
        </beans:bean>

    3.继承LoginUrlAuthenticationEntryPoint  (MyLoginUrlAuthenticationEntryPoint.java)

    public void commence(HttpServletRequest request, HttpServletResponse response,
                    AuthenticationException authException) throws IOException, ServletException {
             // 如果是ajax请求
                if (RequestUtil.isAjaxRequest(request)) {
                    String key = ErrorCodes.BUSINESS_EXCEPTION_PREFIX + ErrorCodes.INVALID_SESSION;
                    Object[] args = new String[0];
                    String message = messageSource.getMessage(key, args, key, LocaleContextHolder.getLocale());
    
                    String jsonObject = "{"message":"" + message + "","
                            + ""needlogin":true,"cause":"Access is denied"}";
                    ResponseUtil.writeJson(response, jsonObject);
                    return;
                } else {
                    super.commence(request, response, authException);
                }
            }

    4.js 扩展ajax

    success:function(data, textStatus){
            //成功回调方法增强处理
            if (data.denied) {
                wms.frame.notifyWarning(data.message);
            } else if (data.needlogin) {
                   wms.frame.notifyWarning(data.message);
            } else{
                   fn.success(data, textStatus);
                  }
            }
    每一步脚印都要扎得深一点!
  • 相关阅读:
    企业移动化?AppCan教你正确的打开方式
    企业该如何挑选移动平台?
    除了移动开发,一个好平台还需要具备什么功能?
    canvas绘制工作流之绘制节点
    canvas与工作流的不解之缘
    一个工作流引擎诞生前的准备工作
    欢迎大家Follow me!微软MVP罗勇(Dynamics CRM方向2015-2018年)欢迎您!
    Dynamics 365定制:在实体的列表界面添加按钮
    Dynamics 365 Customer Engagement中自定义工作流活动的调试
    Dynamics CRM 2015/2016新特性之二十七:使用Web API查询元数据
  • 原文地址:https://www.cnblogs.com/bloodthirsty/p/4891901.html
Copyright © 2011-2022 走看看