使用基本 spring 引导身份验证时出错
Error with using basic spring boot authentication
我正在学习 spring 安全教程,我正在尝试使用 spring-boot-starter-security 使用 class 配置基本安全性,如下所示
import org.springframework.beans.factory.annotation.Autowired;
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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/","/api/*").permitAll()
.anyRequest().authenticated().and().formLogin().loginPage("/login").permitAll()
.and().logout().permitAll();
}
@Autowired
public void ConfigureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(NoOpPasswordEncoder.getInstance())
.withUser("user").password("password").roles("USER");
}
}
但是当我尝试使用 localhost:8080 或 localhost:8080/api/hello?name=test 页面时,它不要求登录并直接显示请求的页面。请帮我解决这个问题。
代码允许访问而无需验证 / 和 /api/*
http.authorizeRequests().antMatchers("/","/api/*").permitAll()
它限制对任何与上述不匹配的请求进行未经身份验证的访问url
.anyRequest().authenticated()
我正在学习 spring 安全教程,我正在尝试使用 spring-boot-starter-security 使用 class 配置基本安全性,如下所示
import org.springframework.beans.factory.annotation.Autowired;
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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/","/api/*").permitAll()
.anyRequest().authenticated().and().formLogin().loginPage("/login").permitAll()
.and().logout().permitAll();
}
@Autowired
public void ConfigureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(NoOpPasswordEncoder.getInstance())
.withUser("user").password("password").roles("USER");
}
}
但是当我尝试使用 localhost:8080 或 localhost:8080/api/hello?name=test 页面时,它不要求登录并直接显示请求的页面。请帮我解决这个问题。
代码允许访问而无需验证 / 和 /api/*
http.authorizeRequests().antMatchers("/","/api/*").permitAll()
它限制对任何与上述不匹配的请求进行未经身份验证的访问url
.anyRequest().authenticated()