【优化】 公共部分、细节优化

This commit is contained in:
wangiegie@gmail.com 2017-10-29 17:51:00 +08:00
parent 39469e857a
commit a065e34309
1 changed files with 50 additions and 34 deletions

View File

@ -1,10 +1,16 @@
package com.github.pig.auth.serivce; package com.github.pig.auth.serivce;
import com.github.pig.auth.mapper.SysUserMapper;
import com.github.pig.common.vo.SysRole;
import com.github.pig.common.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
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.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
@ -19,25 +25,33 @@ import java.util.List;
*/ */
@Service("userDetailService") @Service("userDetailService")
public class UserDetailServiceImpl implements UserDetailsService, Serializable { public class UserDetailServiceImpl implements UserDetailsService, Serializable {
@Autowired
private SysUserMapper sysUserMapper;
@Override @Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException { public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserVo userVo = sysUserMapper.selectUserVoByUsername(username);
if (userVo != null) {
return new UserDetails() { return new UserDetails() {
@Override @Override
public Collection<? extends GrantedAuthority> getAuthorities() { public Collection<? extends GrantedAuthority> getAuthorities() {
List<GrantedAuthority> grantedAuthorities = new ArrayList<>(); List<GrantedAuthority> authorityList = new ArrayList<>();
GrantedAuthority grantedAuthority = (GrantedAuthority) () -> "ROLE_ADMIN"; if (!CollectionUtils.isEmpty(userVo.getRoleList())) {
grantedAuthorities.add(grantedAuthority); for (SysRole role : userVo.getRoleList()) {
return grantedAuthorities; authorityList.add(new SimpleGrantedAuthority(role.getRoleCode()));
}
}
return authorityList;
} }
@Override @Override
public String getPassword() { public String getPassword() {
return "admin"; return userVo.getPassword();
} }
@Override @Override
public String getUsername() { public String getUsername() {
return "admin"; return userVo.getUsername();
} }
@Override @Override
@ -61,4 +75,6 @@ public class UserDetailServiceImpl implements UserDetailsService, Serializable {
} }
}; };
} }
return null;
}
} }