Java 使用带注释的数据库登录Spring安全表单
我有一个Java 使用带注释的数据库登录Spring安全表单,java,spring,spring-security,Java,Spring,Spring Security,我有一个登录页面,在使用内存身份验证时可以正常工作。 但是当我想使用jdbc身份验证时,它不起作用,我也不知道为什么 下面是我的应用程序类的代码 @EnableAutoConfiguration @Configuration @ComponentScan public class App { public static void main(String[] args) throws Throwable { SpringApplication.run(App.class, args);
登录页面
,在使用内存身份验证时可以正常工作。
但是当我想使用jdbc身份验证时,它不起作用,我也不知道为什么
下面是我的应用程序类的代码
@EnableAutoConfiguration
@Configuration
@ComponentScan
public class App {
public static void main(String[] args) throws Throwable {
SpringApplication.run(App.class, args);
}
@Bean(name = "dataSource")
public DriverManagerDataSource dataSource() {
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName("org.postgresql.Driver");
driverManagerDataSource.setUrl("jdbc:postgresql://localhost:5432/bookDB");
driverManagerDataSource.setUsername("postgres");
driverManagerDataSource.setPassword("postgres");
return driverManagerDataSource;
}
@Configuration
@EnableWebMvcSecurity
public class Authentication extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/css/**").permitAll()
.anyRequest().authenticated();
http.formLogin().loginPage("/login").permitAll().and().csrf().disable()
.logout().permitAll();
}
@Autowired
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery(
"select username,password, enabled from users where username=?");
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
下面是我的身份验证类的代码
@EnableAutoConfiguration
@Configuration
@ComponentScan
public class App {
public static void main(String[] args) throws Throwable {
SpringApplication.run(App.class, args);
}
@Bean(name = "dataSource")
public DriverManagerDataSource dataSource() {
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName("org.postgresql.Driver");
driverManagerDataSource.setUrl("jdbc:postgresql://localhost:5432/bookDB");
driverManagerDataSource.setUsername("postgres");
driverManagerDataSource.setPassword("postgres");
return driverManagerDataSource;
}
@Configuration
@EnableWebMvcSecurity
public class Authentication extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/css/**").permitAll()
.anyRequest().authenticated();
http.formLogin().loginPage("/login").permitAll().and().csrf().disable()
.logout().permitAll();
}
@Autowired
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery(
"select username,password, enabled from users where username=?");
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
这里有人能帮我吗?我想它只是缺少了对方法的调用:authorities by usernameQuery
您的代码变成:
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery(
"select username, password, enabled from users where username=?")
**.authoritiesByUsernameQuery(
"select username, role from user_roles where username = ?");**
}
任何错误或消息日志?什么不工作?登录页面不工作;没有错误,但日志为:找不到MessageSource的ResourceBundle[messages]:找不到基本名称消息的bundle,locale en_US