栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Security 会话共享

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Security 会话共享

会话共享
  • 依赖


  org.springframework.boot
  spring-boot-starter-data-redis


  org.springframework.session
  spring-session-data-redis

  • 配置redis
spring:
	redis:
		host: localhost
		port: 6379
  • 配置security
	// 注入

    private final FindByIndexNameSessionRepository sessionRepository;

    @Bean
    public SessionRegistry sessionRegistry() {
        return new SpringSessionBackedSessionRegistry(sessionRepository);
    }
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
		 http.sessionManagement() // 开启会话管理
                .maximumSessions(1) // 最大同时登陆数
                // .and().invalidSessionUrl("invalidSession") // 错误session处理
                // 属于SessionManagementFilter重定向
                // .expiredUrl("/expired") // 被挤下线的会话过期处理 属于ConcurrentSessionFilter将重定向到expiredUrl
                .expiredSessionStrategy( // 过期策略(前后端分离)
                        event -> {
                            HttpServletResponse response = event.getResponse();
                            response.setContentType(MediaType.APPLICATION_JSON_VALUE);
                            String json =
                                    "{"success":false,"message":"SESSION_INVALID","code":401}";
                            response.getWriter().println(json);
                        })
                .maxSessionsPreventsLogin(true) // 登陆后禁止再次登陆(不允许二次登陆)
                // session 共享
                .sessionRegistry(sessionRegistry())
        ;
    }
会话次数限制Bug

如果继承 UserDetails 的实体类没有重写 equals 和 hashCode 方法, 或调用了父类的方法 (super), 但父类没重写, 导致比对失败, 可以无限制登陆.

解决办法出处: https://stackoverflow.com/questions/51916715/concurrent-session-management-not-working-i-have-followed-the-documentation-bu

跨域
  • 单独: @CrossOrigin(origins=“http://允许的域”) 注解(类、方法)
  • 全局: addCrosMapping 重写WebMvcCofingurerComposite#addCorsMapping 方法来实现
 
  • 全局: 过滤器Filter方案
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/927265.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号