Java Spring4安全jdbc身份验证

Java Spring4安全jdbc身份验证,java,spring,spring-security,Java,Spring,Spring Security,我正在尝试让JDBC身份验证与我的小项目一起工作,从外观上看,它应该可以工作,但实际上不行。所有配置如下所示 如果我切换到具有相同用户名/密码的inMemory auth,它将非常有效 这是我记录输出时得到的结果: AuthenticationManagerBuilder配置: @Autowired public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception { auth.j

我正在尝试让JDBC身份验证与我的小项目一起工作,从外观上看,它应该可以工作,但实际上不行。所有配置如下所示

如果我切换到具有相同用户名/密码的inMemory auth,它将非常有效

这是我记录输出时得到的结果:

AuthenticationManagerBuilder配置:

@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {

    auth.jdbcAuthentication().dataSource(dataSource)
            .usersByUsernameQuery("select user_name as username, password, enabled from gag.users as u where u.user_name=?")
            .authoritiesByUsernameQuery("select user_name as username, role from gag.user_roles as u where u.user_name=?");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
    // @formatter:off
    http.authorizeRequests()
            .antMatchers("/").permitAll()
            .antMatchers("/post/**").hasRole("USER")
            .and()
                .formLogin()
                    .loginPage("/login")
            .and()
                .exceptionHandling().accessDeniedPage("/denied")
            .and()
                .csrf().disable();
    // @formatter:on
}
HttpSecurity配置:

@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {

    auth.jdbcAuthentication().dataSource(dataSource)
            .usersByUsernameQuery("select user_name as username, password, enabled from gag.users as u where u.user_name=?")
            .authoritiesByUsernameQuery("select user_name as username, role from gag.user_roles as u where u.user_name=?");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
    // @formatter:off
    http.authorizeRequests()
            .antMatchers("/").permitAll()
            .antMatchers("/post/**").hasRole("USER")
            .and()
                .formLogin()
                    .loginPage("/login")
            .and()
                .exceptionHandling().accessDeniedPage("/denied")
            .and()
                .csrf().disable();
    // @formatter:on
}
数据库表:

CREATE TABLE gag.USERS(
    id SERIAL PRIMARY KEY,
    user_name varchar(30) UNIQUE,
    password varchar(30),
    enabled BOOLEAN NOT NULL DEFAULT TRUE
);

CREATE TABLE gag.USER_ROLES(
    id SERIAL PRIMARY KEY,
    user_name varchar(30) REFERENCES gag.USERS(user_name) NOT NULL,
    role varchar(30) NOT NULL,
    UNIQUE(user_name, role)
);

INSERT INTO gag.USERS(user_name, password, enabled) VALUES('admin', 'admin', TRUE);
INSERT INTO gag.USER_ROLES(user_name, role) VALUES('admin', 'USER');

你知道我为什么要为一个拥有正确角色的用户获得403吗?

自4版以来,Spring Security framework自动添加前缀
角色
。请参阅有关从Spring Security 3.x迁移到4.x的相关文档:

  • :

    SpringSecurity4会自动在任何角色前面加上角色前缀。这些更改是作为

    因此,您必须将插入更改为:

    INSERT INTO gag.USER_ROLES(user_name, role) VALUES('admin', 'ROLE_USER');
    
  • 如果您想省略
    角色
    前缀,您可能会发现上面链接的文章很有趣


自4版以来,Spring Security framework自动添加前缀
角色
。请参阅有关从Spring Security 3.x迁移到4.x的相关文档:

  • :

    SpringSecurity4会自动在任何角色前面加上角色前缀。这些更改是作为

    因此,您必须将插入更改为:

    INSERT INTO gag.USER_ROLES(user_name, role) VALUES('admin', 'ROLE_USER');
    
  • 如果您想省略
    角色
    前缀,您可能会发现上面链接的文章很有趣

hasRole('USER')
转换为检查用户没有的
ROLE\u USER
。将数据库中的
USER
值更改为
ROLE\u USER
,或者使用
hasPermission
hasRole('USER')
转换为检查用户没有的
ROLE\u USER
。将数据库中的
USER
值更改为
ROLE\u USER
,或者使用
hasPermission
代替
hasRole