|
@@ -1,6 +1,8 @@
|
|
|
package com.uas.platform.b2b.support;
|
|
package com.uas.platform.b2b.support;
|
|
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
|
|
|
+import java.util.Collection;
|
|
|
|
|
+import java.util.HashSet;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.dao.DataAccessException;
|
|
import org.springframework.dao.DataAccessException;
|
|
@@ -10,7 +12,11 @@ import org.springframework.security.core.userdetails.User;
|
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
|
|
|
|
+import com.uas.platform.b2b.model.Authority;
|
|
|
|
|
+import com.uas.platform.b2b.model.Resource;
|
|
|
|
|
+import com.uas.platform.b2b.model.Role;
|
|
|
import com.uas.platform.b2b.service.UserService;
|
|
import com.uas.platform.b2b.service.UserService;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -31,7 +37,7 @@ public class CustomUserDetailsService implements UserDetailsService {
|
|
|
private UserService userService;
|
|
private UserService userService;
|
|
|
|
|
|
|
|
static final String TEL_REGEXP = "^((\\(\\d{3}\\))|(\\d{3}\\-))?(13|15|18)\\d{9}$";
|
|
static final String TEL_REGEXP = "^((\\(\\d{3}\\))|(\\d{3}\\-))?(13|15|18)\\d{9}$";
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
static final String UU_REGEXP = "^\\d{4,}$";
|
|
static final String UU_REGEXP = "^\\d{4,}$";
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -47,11 +53,10 @@ public class CustomUserDetailsService implements UserDetailsService {
|
|
|
userinfo = userService.findUserByUserEmail(username);
|
|
userinfo = userService.findUserByUserEmail(username);
|
|
|
} else if (username.matches(TEL_REGEXP)) {// 手机号登录
|
|
} else if (username.matches(TEL_REGEXP)) {// 手机号登录
|
|
|
userinfo = userService.findUserByUserTel(username);
|
|
userinfo = userService.findUserByUserTel(username);
|
|
|
- } else if(username.matches(UU_REGEXP)){
|
|
|
|
|
|
|
+ } else if (username.matches(UU_REGEXP)) {
|
|
|
userinfo = userService.findUserByUserUU(Long.parseLong(username));
|
|
userinfo = userService.findUserByUserUU(Long.parseLong(username));
|
|
|
}
|
|
}
|
|
|
- ArrayList<GrantedAuthority> array = new ArrayList<GrantedAuthority>();
|
|
|
|
|
- array.add(new SimpleGrantedAuthority(ROLE_USER));
|
|
|
|
|
|
|
+ Collection<GrantedAuthority> array = getGrantedAuthorities(userinfo);
|
|
|
User user = new User(String.valueOf(userinfo.getUserUU()), userinfo.getUserPwd(), true, true, true, true, array);
|
|
User user = new User(String.valueOf(userinfo.getUserUU()), userinfo.getUserPwd(), true, true, true, true, array);
|
|
|
currentUser.set(user);
|
|
currentUser.set(user);
|
|
|
return user;
|
|
return user;
|
|
@@ -59,4 +64,29 @@ public class CustomUserDetailsService implements UserDetailsService {
|
|
|
throw new UsernameNotFoundException(username + " 不存在的账号!");
|
|
throw new UsernameNotFoundException(username + " 不存在的账号!");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ private Set<GrantedAuthority> getGrantedAuthorities(com.uas.platform.b2b.model.User user) {
|
|
|
|
|
+ Set<GrantedAuthority> authSet = new HashSet<GrantedAuthority>();
|
|
|
|
|
+ Set<Role> roles = user.getRoles();
|
|
|
|
|
+ if (!CollectionUtils.isEmpty(roles)) {
|
|
|
|
|
+ for (Role role : roles) {
|
|
|
|
|
+ if (role.isSys()) {// 超级账号
|
|
|
|
|
+ authSet.add(new SimpleGrantedAuthority("/**"));
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ Set<Authority> authorities = role.getAuthorities();
|
|
|
|
|
+ if (!CollectionUtils.isEmpty(authorities)) {
|
|
|
|
|
+ for (Authority authority : authorities) {
|
|
|
|
|
+ Set<Resource> resources = authority.getResources();
|
|
|
|
|
+ if (!CollectionUtils.isEmpty(resources)) {
|
|
|
|
|
+ for (Resource res : resources) {
|
|
|
|
|
+ authSet.add(new SimpleGrantedAuthority(res.getName()));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return authSet;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|