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

标签 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;
    }
}

最佳答案

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

像这样

关于spring-security - Spring Security 角色前缀和自定义用户详细信息服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4951832/

相关文章:

spring - 原因 : No AuthenticationProvider found for org. springframework.security.authentication.UsernamePasswordAuthenticationToken

spring-boot - 是否有使用 WebFlux 的 OAuth2 工作示例

PostgreSQL:不允许角色登录

amazon-web-services - 如何使用 AWS CLI 列出所有角色的标签

angular - 您如何使用 MSAL-ANGULAR 读取角色/权限

jsp - 如何让jsp中的spring security角色层次结构起作用?

java - 在 Spring 中如何从 "<sec:authentication property="principal.username"/>"打印用户的名字和姓氏

java - 将 Spring Security 与线程池结合使用会导致重用线程时出现竞争条件

asp.net - Intratnet ASP.NET/SQL2K5 环境的角色安全最佳实践是什么?