我搜索了一些禁用csrf的代码,以便我可以访问“/h2-console”,但它们似乎不工作。当我输入“/h2-console”时,登录提示符仍然出现,就像我编写此代码之前一样
有什么问题吗?
我想在没有登录提示的情况下访问h2数据库
这是安全配置代码,镜像是文件结构(intellij)enter image description here
package com.taxiWithBack.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public BCryptPasswordEncoder encodePwd(){
return new BCryptPasswordEncoder();
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeHttpRequests(authorize -> authorize
.requestMatchers("/h2-console/**").permitAll()
.requestMatchers("/admin").hasRole("ADMIN")
.requestMatchers("/").permitAll()
.anyRequest().authenticated()
)
.formLogin()
.permitAll();
return http.build();
}
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password(encodePwd().encode("password")).roles("USER")
.and()
.withUser("admin").password(encodePwd().encode("password")).roles("ADMIN");
}
}
enter image description here
字符串
1条答案
按热度按时间gzjq41n41#
你能试着修改你的代码,并验证它吗?
字符串
注意事项: