提交 e6779e6f 编写于 作者: sq3536's avatar sq3536

适配redis改造

上级 9429f09a
......@@ -79,11 +79,14 @@ public class SysPSSystemAspect
newIds.add(sysPermission.getPermissionid());
});
//移除无效资源
sysPermissionService.removeBatch(delPermission.keySet());
if(delPermission.size()>0)
sysPermissionService.removeBatch(delPermission.keySet());
//将当前系统本次资源enable设为1以避免enable=0时,导致saveOrUpdate无法检测到主键存在,最终插入数据导致主键重复
sysPermissionService.execute(String.format("update ibzpermission set enable = 1 where sys_permissionid in (%s)",getIds(newIds)),null);
if(newIds.size()>0)
sysPermissionService.execute(String.format("update ibzpermission set enable = 1 where sys_permissionid in (%s)",getIds(newIds)),null);
//存储或更新资源saveOrUpdate
sysPermissionService.saveBatch(list);
if(list.size()>0)
sysPermissionService.saveBatch(list);
}
......
package cn.ibizlab.core.uaa.extensions.service;
import cn.ibizlab.util.client.IBZOUFeignClient;
import cn.ibizlab.util.service.AuthenticationUserService;
import cn.ibizlab.util.service.IBZUSERService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ibizlab.util.security.AuthenticationUser;
import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.util.helper.CachedBeanCopier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import cn.ibizlab.util.mapper.IBZUSERMapper;
import cn.ibizlab.util.domain.IBZUSER;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* 实体[IBZUSER] 服务对象接口实现
*/
@Primary
@Service("UAAUserService")
public class UAAUserService extends ServiceImpl<IBZUSERMapper, IBZUSER> implements IBZUSERService, AuthenticationUserService {
@Value("${ibiz.auth.pwencrymode:0}")
private int pwencrymode;
@Override
public AuthenticationUser loadUserByUsername(String username) {
if(StringUtils.isEmpty(username))
throw new UsernameNotFoundException("用户名为空");
QueryWrapper<IBZUSER> conds=new QueryWrapper<IBZUSER>();
String[] data=username.split("[|]");
String loginname="";
String domains="";
if(data.length>0)
loginname=data[0].trim();
if(data.length>1)
domains=data[1].trim();
if(!StringUtils.isEmpty(loginname))
conds.eq("loginname",loginname);
if(!StringUtils.isEmpty(domains))
conds.eq("domains",domains);
IBZUSER user = this.getOne(conds);
if (user == null) {
throw new UsernameNotFoundException("用户" + username + "未找到");
}
else {
user.setUsername(username);
AuthenticationUser curUser = createUserDetails(user);
if(curUser.getPermissionList()==null)
{
setUserPermission(curUser);
setUserOrgInfo(curUser);
}
return curUser;
}
}
@Override
public AuthenticationUser loadUserByLogin(String username, String password){
AuthenticationUser authuserdetail = loadUserByUsername(username);
if(pwencrymode==1)
password = DigestUtils.md5DigestAsHex(password.getBytes());
else if(pwencrymode==2)
password = DigestUtils.md5DigestAsHex(String.format("%1$s||%2$s", username, password).getBytes());
if(!authuserdetail.getPassword().equals( password )){
throw new BadRequestAlertException("用户名密码错误","IBZUSER",username);
}
return authuserdetail;
}
public void resetByUsername(String username) {
}
public AuthenticationUser createUserDetails(IBZUSER user) {
AuthenticationUser userdatail = new AuthenticationUser();
CachedBeanCopier.copy(user,userdatail);
if(userdatail.getSuperuser()==1){
userdatail.setAuthorities(AuthorityUtils.createAuthorityList("ROLE_SUPERADMIN"));
}
return userdatail;
}
@Autowired
@Lazy
private UAACoreService uaaCoreService;
/**
* 设置用户权限
* 由于GrantedAuthority缺少无参构造,导致无法序列化,暂时通过PermissionList中转
* @param user
* @return
*/
public void setUserPermission(AuthenticationUser user) {
Collection<GrantedAuthority> userAuthorities=uaaCoreService.getAuthoritiesByUserId(user.getUserid());
Set<String> authorities = AuthorityUtils.authorityListToSet(userAuthorities);
if(user.getSuperuser()==1){
authorities.add("ROLE_SUPERADMIN");
}
JSONObject permission =new JSONObject();
permission.put("authorities",authorities);
user.setPermissionList(permission);
}
@Autowired
@Lazy
private IBZOUFeignClient ouFeignClient;
/**
* 设置用户组织相关信息
* @param user
*/
private void setUserOrgInfo(AuthenticationUser user) {
Map<String, Set<String>> orgInfo=ouFeignClient.getOUMapsByUserId(user.getUserid());
if(orgInfo==null)
orgInfo=new HashMap<>();
//throw new RuntimeException(String.format("获取用户信息失败,请检查用户中心[IBZOU]中是否存在[%s]用户!",user.getLoginname()));
user.setOrgInfo(orgInfo);
}
}
\ No newline at end of file
......@@ -34,21 +34,16 @@ public class ClientAuthenticationResource
private AuthTokenUtil jwtTokenUtil;
@Autowired
@Qualifier("IBZUSERService")
@Qualifier("UAAUserService")
private AuthenticationUserService userDetailsService;
@Autowired
private IBZOUFeignClient ouFeignClient;
@PostMapping(value = "v7/login")
public ResponseEntity<AuthenticationInfo> login(@Validated @RequestBody AuthorizationLogin authorizationLogin){
userDetailsService.resetByUsername(authorizationLogin.getUsername());
AuthenticationUser user = userDetailsService.loadUserByLogin(authorizationLogin.getUsername(),authorizationLogin.getPassword());
if(user.getPermissionList()==null)
{
setUserPermission(user);
setUserOrgInfo(user);
}
final String token = jwtTokenUtil.generateToken(user);
// 返回 token
return ResponseEntity.ok().body(new AuthenticationInfo(token,user));
......@@ -58,59 +53,14 @@ public class ClientAuthenticationResource
public ResponseEntity<AuthenticationUser> uaalogin(@Validated @RequestBody AuthorizationLogin authorizationLogin){
userDetailsService.resetByUsername(authorizationLogin.getUsername());
AuthenticationUser user = userDetailsService.loadUserByLogin(authorizationLogin.getUsername(),authorizationLogin.getPassword());
if(user.getPermissionList()==null)
{
setUserPermission(user);
setUserOrgInfo(user);
}
return ResponseEntity.ok().body(user);
}
@PostMapping(value = "uaa/loginbyusername")
public ResponseEntity<AuthenticationUser> loginByUsername(@Validated @RequestBody String username){
//userDetailsService.resetByUsername(authorizationLogin.getUsername());
AuthenticationUser user = userDetailsService.loadUserByUsername(username);
if(user.getPermissionList()==null)
{
setUserPermission(user);
setUserOrgInfo(user);
}
return ResponseEntity.ok().body(user);
}
@Autowired
UAACoreService uaaCoreService;
/**
* 设置用户权限
* 由于GrantedAuthority缺少无参构造,导致无法序列化,暂时通过PermissionList中转
* @param user
* @return
*/
public void setUserPermission(AuthenticationUser user) {
Collection<GrantedAuthority> userAuthorities=uaaCoreService.getAuthoritiesByUserId(user.getUserid());
Set<String> authorities = AuthorityUtils.authorityListToSet(userAuthorities);
if(user.getSuperuser()==1){
authorities.add("ROLE_SUPERADMIN");
}
JSONObject permission =new JSONObject();
permission.put("authorities",authorities);
user.setPermissionList(permission);
}
/**
* 设置用户组织相关信息
* @param user
*/
private void setUserOrgInfo(AuthenticationUser user) {
Map<String, Set<String>> orgInfo=ouFeignClient.getOUMapsByUserId(user.getUserid());
if(orgInfo==null)
orgInfo=new HashMap<>();
//throw new RuntimeException(String.format("获取用户信息失败,请检查用户中心[IBZOU]中是否存在[%s]用户!",user.getLoginname()));
user.setOrgInfo(orgInfo);
}
}
......@@ -17,8 +17,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
/**
* 实体[IBZUSER] 服务对象接口实现
*/
@Service("IBZUAAUserService")
@ConditionalOnExpression("${ibiz.enablePermissionValid:false}||'${ibiz.auth.service:SimpleUserService}'.equals('IBZUAAUserService')")
//@Service("IBZUAAUserService")
//@ConditionalOnExpression("${ibiz.enablePermissionValid:false}||'${ibiz.auth.service:SimpleUserService}'.equals('IBZUAAUserService')")
public class IBZUAAUserService implements AuthenticationUserService{
@Autowired
......
......@@ -19,8 +19,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
/**
* 实体[IBZUSER] 服务对象接口实现
*/
@Primary
@Service("IBZUSERService")
//@Primary
//@Service("IBZUSERService")
public class IBZUSERServiceImpl extends ServiceImpl<IBZUSERMapper, IBZUSER> implements IBZUSERService,AuthenticationUserService{
@Value("${ibiz.auth.pwencrymode:0}")
......
......@@ -16,8 +16,8 @@ import org.springframework.security.core.authority.AuthorityUtils;
/**
* 实体[IBZUSER] 服务对象接口实现
*/
@Service("SimpleUserService")
@ConditionalOnExpression("(!${ibiz.enablePermissionValid:false})&&'${ibiz.auth.service:SimpleUserService}'.equals('SimpleUserService')")
//@Service("SimpleUserService")
//@ConditionalOnExpression("(!${ibiz.enablePermissionValid:false})&&'${ibiz.auth.service:SimpleUserService}'.equals('SimpleUserService')")
public class SimpleUserService implements AuthenticationUserService{
@Override
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册