无法通过"Authentication Required"登录弹出窗口到我的 H2 控制台



我正在使用一个 H2 内存数据库,当我禁用安全性时,其中存储了一些用户数据。

但是,当我重新启用安全性时,我有此身份验证弹出窗口,并且我没有正确的用户名和密码来通过它。

我注意到每次当我在身份验证弹出窗口中输入并单击"确定"时,都会显示一条 SQL 消息: 休眠:选择 userinfo0_.user_id 作为 user_id1_1_,userinfo0_.password 作为password2_1_,userinfo0_.user_name 作为user_info userinfo0_user_nam3_1_,其中 userinfo0_.user_name=?表格和列信息与我的 H2 的表格设置相匹配。我认为这应该表明它正在从我的数据库中查找。

但是,即使我输入了正确的用户名和密码,它也不会让我进入。

下面是我的配置类

@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userInfoService;
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.requestMatchers().antMatchers("/login", "/logout", "/oauth/authorize", "/oauth/confirm_access")
.and()
.authorizeRequests()
.antMatchers("/h2","/h2_console/**").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").permitAll();
http.csrf().disable();
http.headers().frameOptions().disable();
// @formatter:on
}
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password("user").roles("USER")
.and()
.withUser("admin").password("admin").roles("ADMIN");
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider());
}
@Bean
public DaoAuthenticationProvider authenticationProvider(){
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
authenticationProvider.setUserDetailsService(this.userInfoService);
authenticationProvider.setPasswordEncoder(passwordEncoder());
return authenticationProvider;
}
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}

我在数据库中存储了多个用户,其中一些用户的密码为明文,而另一个用户则使用上述编码器的加密密码。

我唯一的怀疑是数据库中的用户没有管理员角色来访问/h2 路径(H2 控制台(。

无论如何,我应该如何通过此弹出窗口访问我的 h2 控制台?

我已经有一段时间没有使用 h2 了。我很确定您不必在数据库中使用凭据。相反,您必须在 application.properties 文件中设置凭据。

spring.h2.console.enabled=true
spring.h2.console.path=/h2
spring.datasource.url=jdbc:h2:mem:yourdb;MODE=Oracle
spring.datasource.username=sa
spring.datasource.password=

尝试使用用户名sa和空白密码登录。

csrf((.disble((可能需要成为HTTP调用的第一个方法之一。当我在开始时添加它时,我不再需要进行身份验证,也许有一天我当前的文件可能会帮助某人:

http.cors().and().httpBasic()
.and().csrf().disable()
.authorizeRequests()
.antMatchers("/nonRegistered/**").permitAll()
.antMatchers("/person/**").hasAuthority("USER")
.antMatchers("/doctor/**").hasAuthority("DOCTOR")
.antMatchers("/accommodation/**").hasAuthority("ACCOMMODATION")
.antMatchers("/borderPatrol/**").hasAuthority("BORDERPATROL")
.antMatchers("/console/**").permitAll() // h2 console link
.antMatchers("/admin/**").hasAuthority("ADMIN")
.anyRequest().fullyAuthenticated()
;
http.headers().frameOptions().disable();// needed to h2 console to work

快乐的骗子;)

最新更新