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

发布于 2024-10-16 20:27:34 字数 1694 浏览 2 评论 0原文

如何在 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;
    }
}

How do I set the role prefix to "" with a custom user details service in 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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秉烛思 2024-10-23 20:27:34
<beans:bean id="roleVoter" class="org.springframework.security.access.vote.RoleVoter">
    <beans:property name="rolePrefix" value="" />
</beans:bean>

就像这样

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

just like this

逐鹿 2024-10-23 20:27:34

还可以使用映射器将 _ROLE 附加到您当前的角色。在 Spring Boot 中:

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

    return simpleMapper;
}

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

@Bean
public DaoAuthenticationProvider authenticationProvider() {
    DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
    provider.setAuthoritiesMapper(authoritiesMapper());

    return provider;
}

It's also possible to append a _ROLE to your current roles using a mapper. In Spring Boot:

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

    return simpleMapper;
}

After that you should add this mapper to your provider:

@Bean
public DaoAuthenticationProvider authenticationProvider() {
    DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
    provider.setAuthoritiesMapper(authoritiesMapper());

    return provider;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文