SpringSecurity 用户切换员工

### 现在有一个需求就是用户要切换不同的公司

我们有两张表一张User表一张Emp表

User表不需多说  两个字段  acct账号   mobile 手机号,用户通过这两个值都可以登录

Emp绑定了User表   有一个字段 UserId

Emp绑定了公司,绑定了权限

由于我们公司没有DBA,在我们需求人员的设计下的数据库,而我们使用了某公司的框架,而框架中的代码都是写死的,而且通过UserDetailService只会传一个值 (SpringSecurity)

就有了以下的代码

Controller

OAuth2Authentication authentication = userChekEmpService.checkEmp(empDTO,getLoginEmpId());
//打开Sessino设置权限
SecurityContext context = SecurityContextHolder.getContext();
context.setAuthentication(authentication);
HttpSession session = request.getSession(true);
session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, context);  

Service

    Emp emp = empPremMapper.getEmpInfoById(empDTO.getEmpId());
    /*
      检查用户是否绑定该员工,抛异常
    */
    //有员工就一定有用户     UserEntity userInfo = userQueryService.getByUserAcct(emp.getEmpTel()); userInfo.setId(emp.getEmpId()); //Emp Authorities UserDetails userDetails = userServiceComposite.buildAuthorities(userInfo); UsernamePasswordAuthenticationToken empAuthenticationToken = new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities()); //Token //当前的的OAuth2Authentication OAuth2Authentication loginAuthentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication(); //UserDetails Object details = loginAuthentication.getDetails(); empAuthenticationToken.setDetails(details); String token = ((OAuth2AuthenticationDetails) details).getTokenValue(); OAuth2AccessToken accessToken = tokenStore.readAccessToken(token); OAuth2Authentication empAuthentication = new OAuth2Authentication(loginAuthentication.getOAuth2Request(), empAuthenticationToken);
     /*
        更新redis缓存,刷新权限,
RefreshToken,AccecToken
     */
     return empAuthentication;

 

posted @ 2021-02-27 15:54  AngelXing  阅读(491)  评论(0)    收藏  举报