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

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,46 +25,56 @@ 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 {
return new UserDetails() { UserVo userVo = sysUserMapper.selectUserVoByUsername(username);
@Override if (userVo != null) {
public Collection<? extends GrantedAuthority> getAuthorities() { return new UserDetails() {
List<GrantedAuthority> grantedAuthorities = new ArrayList<>(); @Override
GrantedAuthority grantedAuthority = (GrantedAuthority) () -> "ROLE_ADMIN"; public Collection<? extends GrantedAuthority> getAuthorities() {
grantedAuthorities.add(grantedAuthority); List<GrantedAuthority> authorityList = new ArrayList<>();
return grantedAuthorities; if (!CollectionUtils.isEmpty(userVo.getRoleList())) {
} for (SysRole role : userVo.getRoleList()) {
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
public boolean isAccountNonExpired() { public boolean isAccountNonExpired() {
return true; return true;
} }
@Override @Override
public boolean isAccountNonLocked() { public boolean isAccountNonLocked() {
return true; return true;
} }
@Override @Override
public boolean isCredentialsNonExpired() { public boolean isCredentialsNonExpired() {
return true; return true;
} }
@Override @Override
public boolean isEnabled() { public boolean isEnabled() {
return true; return true;
} }
}; };
}
return null;
} }
} }