Spring security 用弹簧靴支撑+;具有自定义用户表的spring安全性

Spring security 用弹簧靴支撑+;具有自定义用户表的spring安全性,spring-security,spring-boot,Spring Security,Spring Boot,我想为一些rest服务配置Spring引导和Spring安全性。 我有表用户,特权用户,特权用户 我有这个配置文件 网络安全配置 @EnableWebSecurity @Configuration class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception {

我想为一些rest服务配置Spring引导和Spring安全性。 我有表用户,特权用户,特权用户 我有这个配置文件

网络安全配置

@EnableWebSecurity
@Configuration
class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().fullyAuthenticated().and().
                httpBasic().and().
                csrf().disable();
    }

}
和身份验证配置

@Configuration
public class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter {
    @Autowired
    UserRepository userRepository;
    Logger log = LoggerFactory.getLogger(this.getClass());

    @Override
    public void init(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService());
    }

    @Bean
    UserDetailsService userDetailsService() {
        return new UserDetailsService() {

            @Override
            public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

                User account = userRepository.findByUsername(username);
                log.info("The user ({}) logged has the password {}", username, account.getPassword());
                org.springframework.security.core.userdetails.User userSession =  new org.springframework.security.core.userdetails.User(account.getUsername(), account.getPassword(), true, true, true, true,
                        AuthorityUtils.createAuthorityList("USER"));
                return userSession;
            }

        };
    }
}
调试代码似乎验证工作正常,因为我有来自数据库的用户(我使用的是JNDI数据源,并在application.properties中配置了这个) 但是,当我试图打开任何东西并设置凭据时,我无法访问,并且总是得到提示输入凭据
我做了什么坏事?

您没有使用正确的权限实例化UserDetails对象

您不应该总是将“USER”传递给AuthorityUtils.createAuthorityList方法,而应该为正在加载的用户传递一个包含privilege_USER表中所有角色/权限的列表。默认情况下,此角色的形式为“角色\用户”、“角色\管理员”


因此,从数据库成功加载用户并不意味着身份验证过程工作正常。您仍然必须告诉他如何加载该用户的权限。

问题是,您正在使用带有@CreatedBy或@LastModifiedBy注释的DB audit,但您试图在任何用户登录之前插入文档。当您在登录页控制器中创建新用户或创建操作时,会发生这种情况。为了克服这个问题,在数据库中有一个系统用户,system@yourdomain.com如果输入参数正确,则在相应的控制器中设置principal


执行此操作时,DBAuditor将获取带注释字段的系统用户id。确保在此操作后加载新创建的用户主体

数据库中是否有有效的用户?日志说明了什么?是的,我在数据库中有一个有效的用户,我在日志中没有错误,只有
2015-11-09 17:07:26069信息[org.springframework.boot.actuate.audit.listener.AuditListener](默认任务2)AuditEvent[timestamp=Mon Nov 09 17:07:26 BOT 2015,principal=anonymousUser,type=AUTHORIZATION\u FAILURE,data={type=org.springframework.security.access.AccessDeniedException,message=access is denied}]
因此,我还没有分配数据库中的角色,那么我认为如果我将相同的角色(USER)设置给所有用户,这应该允许访问权限?我需要配置哪个角色应该允许控制器中的某些方法?我在Personcontroller类@PreAuthorize(“用户”)中设置了,但这仍然不起作用尝试在AuthorityUtils.createAuthorityList(“role_USER”)中设置“role_USER”而不是“USER”。我认为数据库musut中的角色具有“role_”前缀,尽管您仍然可以在@PreAuthorize(“用户”)上检查“用户”,因为Spring security默认情况下会添加前缀。另外,尝试只点击localhost:8080,而不是PersonController方法的url,然后查看发生了什么。根据您的配置,这应该会询问您的凭据,但没有验证任何权限。我已将用户更改为角色\用户,但这仍然不起作用。我还注意到另一个问题错误
[INFO[org.springframework.boot.actuate.audit.listener.AuditListener](默认任务-60)AuditEvent[timestamp=Mon Nov 09 20:26:10 EST 2015,principal=anonymousUser,type=AUTHORIZATION\u FAILURE,data={type=org.springframework.security.access.AccessDeniedException,message=AccessDeniedException}][0mtask-23)AuditEvent[timestamp=Mon Nov 09 20:25:42 EST 2015,principal=anonymousUser,type=AUTHORIZATION\u FAILURE,data={type=org.springframework.security.access.AccessDeniedException,message=访问被拒绝}]