IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> 登录超时提示+踢人下线实现(spring security) -> 正文阅读

[Java知识库]登录超时提示+踢人下线实现(spring security)

前言

???????最近,说有可能要上只允许一个地方登录,还要配合信息推送,今天有空,就起个头,把登录超时、登录踢人下线一起做了。信息推送的,后面再说,留好口子就行。


一、背景

这里是spring security,其实这块已经很成熟了,加几个配置就行。

二、使用步骤

1.security配置增加


/**
 * 安全认证配置
 *
 * @author zhengwen
 */
@Configuration
@EnableWebSecurity
@Order(1)
public class LinkappSecurityConfiguration extends WebSecurityConfigurerAdapter {

  @Resource
  private SessionInformationExpiredStrategy sessionInformationExpiredStrategy;

  @Resource
  private InvalidSessionStrategy invalidSessionStrategy;
 

  @Override
  protected void configure(HttpSecurity http) throws Exception {

//		解决 spring security 对于开放接口返回乱码的解决
    CharacterEncodingFilter filter = new CharacterEncodingFilter();
    filter.setEncoding("UTF-8");
    filter.setForceEncoding(true);
    http.addFilterBefore(filter, CsrfFilter.class);
    http.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
    http
        // 禁止匿名用户
        // .anonymous().disable()
        // 禁止csrfz

        .csrf().disable()
        // 认证失败处理
        .exceptionHandling()
        .authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
        .and()
        // 白名单
        .authorizeRequests()
        .antMatchers("/swagger-ui.html", "/webjars/**", "/swagger-resources/**", "/v2/api-docs/**",           "/enterpriseEditionBi/**","/oss/download/**","/download/**","/config/getEduUrl")
        .permitAll()
        // 接口调试阶段 目前不校验接口 modify by tongjie
        .anyRequest().authenticated().and()
        // 表单登录配置
        .formLogin()
        // 登录成功处理
        .successHandler(linkappRestAuthenticationSuccessHandler)
        // 登录失败处理
        .failureHandler(linkappRestAuthenticationFailureHandler).and()
        // 登出成功处理
        .logout().logoutSuccessHandler(linkappRestLogoutSuccessHandler);
      //一个账号只允许一个地方登录,
      http.sessionManagement()
              //session失效,调用此方法
              .invalidSessionStrategy(invalidSessionStrategy).maximumSessions(1)
              // 当用户达到最大session数后,则调用此处的实现
              .expiredSessionStrategy(sessionInformationExpiredStrategy);
  }

    @Bean
    @ConditionalOnMissingBean(InvalidSessionStrategy.class)
    public InvalidSessionStrategy invalidSessionStrategy() {
        return new MyInvalidSessionStrategy();
    }
    @Bean
    @ConditionalOnMissingBean(SessionInformationExpiredStrategy.class)
    public SessionInformationExpiredStrategy informationExpiredStrategy() {
        return new MySessionInformationExpiredStrategy();
    }
}

http.sessionManagement()开始就是本次分享内容。注意下面的2个bean,@Bean注解的。

2.MyInvalidSessionStrategy


/**
 * @author zhengwen
 */
public class MyInvalidSessionStrategy implements InvalidSessionStrategy {
    @Override
    public void onInvalidSessionDetected(HttpServletRequest httpServletRequest, HttpServletResponse response) throws IOException {
        /* 接口请求没有页面,给统一的友好提示,可以直接利用封装的的业务异常
        response.setStatus(HttpStatus.HTTP_UNAUTHORIZED);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write("当前登录已失效!请重新登录");
        */
        throw new BusinessException("当前登录已失效!请重新登录");
    }
}

BusinessException是封装的业务异常类。

3.MySessionInformationExpiredStrategy


/**
 * @author zhengwen
 */
@Slf4j
@Component
public class MySessionInformationExpiredStrategy implements SessionInformationExpiredStrategy {

    @Autowired
    private LinkappRestAuthenticationFailureHandler myAuthenticationFailureHandler;

    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event) {
        // 1. 获取用户名
        UserDetails userDetails =
                (UserDetails) event.getSessionInformation().getPrincipal();

        AuthenticationException exception =
                new AuthenticationServiceException(
                        String.format("[%s]用户在其他地方登录,您已被下线", userDetails.getUsername()));
        //TODO 这里可以增加根据用户名找到手机号信息,进行推送信息,或者在myAuthenticationFailureHandler里去处理
        try {
            // 当用户在另外终端登录后,交给失败处理器回到认证页面
            event.getRequest().setAttribute("toAuthentication", true);
            myAuthenticationFailureHandler
                    .onAuthenticationFailure(event.getRequest(), event.getResponse(), exception);
        } catch (Exception e) {
            log.error("--登录失效session清除异常,原因:{}", e.getMessage());
            throw new BusinessException("超时登录session清除异常");
        }
    }
}


4.MyAuthenticationFailureHandler



/**
 * 认证失败处理者
 *
 * @author zhengwen
 */
@Component
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
 

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

        //TODO 自己的异常处理逻辑

        RestMessage message = RestBuilders.failureBuilder().code("login.failure")
            .message(exception.getMessage()).build();

        Responses.standard(response).respond(message);

    }
    
}

三、看效果

1.先在postman登录成功。

在这里插入图片描述

2.再请求一个业务接口

在这里插入图片描述

3.然后在另外一个地方登录同一个账号,这里用FE的简易postman工具模拟

在这里插入图片描述

4.最后再重新请求下那个业务接口

在这里插入图片描述
多的废话以不想说了额,这一组图看的真真的。


总结

  • spring security用户登录session登录管理真强大,虽然说很重,但是用起来确实方便。
  • 现在spring security不像以前了,它也与时俱进,配置早springBoot里做的很友好了
  • 你们猜.maximumSessions(2)会先踢哪个?
  • 如果要加信息推送知道在那里加了吧?认真看TODO
    好了,就到这吧,UPing!!
  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2022-11-05 00:12:25  更:2022-11-05 00:17:59 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年3日历 -2025/3/10 19:39:12-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码