[英]Error with using basic spring boot authentication
我正在學習 spring 安全教程,我正在嘗試使用 class 使用 spring-boot-starter-security 配置基本安全性,如下所示
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.