2 回答

TA貢獻(xiàn)1725條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以通過配置來配置每個(gè) URL 的安全性 HttpSecurity:
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//Ignore other configuration stuff for simplicity
http.authorizeRequests()
.antMatchers("/sign-up" ,"/verify/**" ).permitAll()
.anyRequest().authenticated()
}
}
然后對(duì) URL 的所有請(qǐng)求除外/sign-up并且/verify/**需要身份驗(yàn)證(在您的情況下這意味著 JWT)。
如果你想進(jìn)一步控制,你甚至可以執(zhí)行以下操作/sign-up,并且/verify/**只能在沒有身份驗(yàn)證的情況下訪問正確的 HTTP 方法:
http.authorizeRequests()
.antMatchers(HttpMethod.POST, "/sign-up").permitAll()
.antMatchers(HttpMethod.GET, "/verify/**").permitAll()
.anyRequest().authenticated()

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以使用以下配置實(shí)現(xiàn)您的要求。這是使用不需要身份驗(yàn)證/授權(quán)的 URL 的好方法WebSecurity using ignoring instead of HttpSecurity as WebScurity will bypass the Spring Security Filter Chain and reduce the execution time
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/sign-up")
.antMatchers("/verify/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/set-password/").hasRole("yourROLE")
.antMatchers("/set-profile").hasRole("yourROLE")
.anyRequest().authenticated();
}
當(dāng)您使用HttpSecurity并嘗試permitAll()請(qǐng)求時(shí)。您的請(qǐng)求將被允許從 Spring Security 過濾器鏈訪問。這是昂貴的,因?yàn)闀?huì)有其他請(qǐng)求也將進(jìn)入此過濾器鏈,需要根據(jù)身份驗(yàn)證/授權(quán)允許或不允許
但是當(dāng)你使用時(shí)WebSecurity,任何請(qǐng)求都sign-up or verify將完全繞過 Spring Security Filter Chain。這是安全的,因?yàn)槟恍枰魏紊矸蒡?yàn)證/授權(quán)就可以查看圖像或讀取 javascript 文件。
添加回答
舉報(bào)