Spring安全角色前缀和自定义用户详细信息服务

时间:2011-02-09 23:33:51

标签: spring-security roles security-roles

如何使用Spring中的自定义用户详细信息服务将角色前缀设置为""

    <beans:bean id="authService" class="com.cisco.badges.business.services.AuthenticationService"/>

<authentication-manager>
        <authentication-provider user-service-ref="authService">
            <password-encoder ref="passwordEncoder">
                <salt-source ref="saltSource" />
            </password-encoder>
        </authentication-provider>
    </authentication-manager>

@Service("authService")
public class AuthenticationService extends BaseService implements UserDetailsService, IAuthenticationService {

    @Autowired
    IUserRepository userRepository;

    @Autowired
    IAuthorityRepository authorityRepository;

    public AuthenticationService() {

    }

    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException {

        User user = userRepository.findByUsername(username);

        if(user == null)
            throw new UsernameNotFoundException("No user with username '" + username + "' found!");

        List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>();

        for (Role role : user.getRoles()) {
            authList.add(new GrantedAuthorityImpl(role.getName()));
        }

        UserAuthentication userAuthentication = new UserAuthentication(user.getUsername(), user.getPassword(), user.getEnabled() == 0 ? false : true, true, true, true, authList);

        userAuthentication.setSalt(user.getSalt());
        userAuthentication.setId(user.getId());

        return (UserDetails)userAuthentication;
    }
}

2 个答案:

答案 0 :(得分:6)

<beans:bean id="roleVoter" class="org.springframework.security.access.vote.RoleVoter">
    <beans:property name="rolePrefix" value="" />
</beans:bean>

就像这样

答案 1 :(得分:0)

还可以使用映射器将<div class="photoseries-content"> <div class="owl-carousel"> <div> <img src="../img/photoseries/iran/iran-1.jpg" alt="Iran"> </div> <div> <img src="../img/photoseries/iran/iran-2.jpg" alt="Iran"> </div> <div> <img src="../img/photoseries/iran/iran-3.jpg" alt="Iran"> </div> <div> <img src="../img/photoseries/iran/iran-4.jpg" alt="Iran"> </div> <div> <img src="../img/photoseries/iran/iran-5.jpg" alt="Iran"> </div> <div> <img src="../img/photoseries/iran/iran-6.jpg" alt="Iran"> </div> <div> <img src="../img/photoseries/iran/iran-7.jpg" alt="Iran"> </div> <div> <img src="../img/photoseries/iran/iran-8.jpg" alt="Iran"> </div> </div> </div>附加到当前角色。在Spring Boot中:

_ROLE

之后,您应该将此映射器添加到您的提供程序中:

@Bean
public GrantedAuthoritiesMapper grantedAuthoritiesMapper() {
    SimpleAuthorityMapper simpleMapper = new SimpleAuthorityMapper();
    simpleMapper.setPrefix("ROLE_");

    return simpleMapper;
}
相关问题