Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package demo.domain.service; import demo.domain.model.Account; import demo.domain.model.Authority; import demo.domain.model.Role; import demo.domain.model.UserProjectRoleAuthority; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; 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.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** * * @author Kanekos */ @Service public class MyUserDetailsService implements UserDetailsService { @Autowired AccountService accountService; @Autowired RoleService roleService; @Autowired AuthorityService authorityService; @Autowired UserProjectRoleAuthorityService userProjectRoleAuthorityService; @Override @Transactional(readOnly = true) public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { Account account = Optional.ofNullable(accountService.selectFindOne(username)) .orElseThrow(() -> new UsernameNotFoundException("?????")); List<UserProjectRoleAuthority> userProjectRoleAuthority = Optional .ofNullable(userProjectRoleAuthorityService.selectFindByUserId(account.getUserId())) .orElseThrow(() -> new UsernameNotFoundException("??????")); List<GrantedAuthority> authorities = new ArrayList<>(); userProjectRoleAuthority.forEach((UserProjectRoleAuthority upra) -> { StringBuilder sb = new StringBuilder(); sb.append(upra.getProjectId()); sb.append("."); sb.append(upra.getRoleId()); sb.append("."); sb.append(upra.getAuthorityId()); authorities.add(new SimpleGrantedAuthority(sb.toString())); }); return new MyUserDetails(account, authorities); } }