我正在嘗試制作一個使用以下內容的 Web 應用程式:SpringBoot、Mysql、JDBC、MVC、DAO Thymeleaf、IntelliJ
而且我正試圖弄清楚 Spring 安全性是如何作業的(我遇到了很多困難)。我的觀點組織如下:
resources(folder): - ________static(folder)
|____templates(folder):__________images(folder)
|___userOnly(folder):_____header.html
| |__help.html
| |__menu.html
| |__newDocForm.html
| |__profil.html
|
|__firstPage.html
|__header.html
|__home.html
|__index.html
|__inscriptionForm.html
|__loginPage.html
我希望身份不明的用戶可以訪問除“userOnly”中包含的視圖之外的所有視圖,并且我的“loginPage”頁面用作登錄頁面。
如果我理解正確,我必須創建一個繼承自“WebSecurityConfigurerAdapter”的類。我做了什么。然后配置“配置”,我無法正確執行:(
@Configuration
@EnableWebSecurity
public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(final HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/userOnly/**").hasRole("USER")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/loginPage.html");
}
}
對不起,如果我的問題看起來很奇怪,但英語不是我的第一語言
uj5u.com熱心網友回復:
從 Spring-Boot 2.7 開始,WebSecurityConfigurerAdapter不推薦使用 。如果您使用的是 spring-boot 2.6 或更早版本,其他答案可能更適合。
據我所知,在 Spring-Boot 2.7 中定義安全配置的推薦方法如下:
@Configuration
public class WebSecurityConfig
{
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception
{
// @formatter:off
http.authorizeRequests()
.mvcMatchers("/userOnly/**").permitAll()
.anyRequest().permitAll();
http.formLogin()
.permitAll()
.loginPage("/loginPage.html");
http.logout()
.permitAll();
// @formatter:on
return http.build();
}
}
voucher_wolves我相信不推薦在答案中使用 web.ignoring() ,而是應該將這些情況添加到http.mvcMatcher().permitAll(). 附帶說明一下,如果您忘記為默認情況下不公開的鏈接添加安全性,我個人建議將公共頁面列入白名單并向其他所有內容(例如公共檔案夾)添加身份驗證。
uj5u.com熱心網友回復:
你需要告訴 Spring security 什么樣的 URL 是公開的,像這樣 -
@Configuration
@EnableWebSecurity
public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
private static final String[] PUBLIC_URLS = {"/public/*"};
@Override
protected void configure(final HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/userOnly/**").hasRole("USER")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/loginPage.html");
}
@Override
public void configure(WebSecurity web) {
List<RequestMatcher> matchers =
Arrays.asList(urls).stream().map(url -> new
AntPathRequestMatcher(url)).collect(Collectors.toList());
web.ignoring().requestMatchers(new OrRequestMatcher(matchers));
}
}
使用OrRequestMatcher,您可以創建需要公開的所有 URL 的串列。您還可以使用NegatedRequestMatcher獲取所有私有 URL
RequestMatcher privateUrlMatcher = new
NegatedRequestMatcher(publicUrlMatcher);
我還建議您將所有公共 url 保留在 /src/main/resources/static/publicui 下,并將所有私有 URL 保留在 /src/main/resources/static/privateui 下,并擁有 /publicui/* 的公共權限
uj5u.com熱心網友回復:
在您的 SecSecurityConfig 類中嘗試以下操作
@Configuration
@EnableAutoConfiguration
@EnableWebSecurity
public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/users").authenticated()
.anyRequest().permitAll()
.and()
.formLogin()
.usernameParameter("email")
.defaultSuccessUrl("/lib/allBooks")
.permitAll()
.and()
.logout().logoutSuccessUrl("/lib").permitAll();
http
.csrf().disable();
}
}
只需修改為您的應用程式設定的引數。如果您沒有登錄表單,您可以跳過
.usernameParameter("email")
.defaultSuccessUrl("/lib/allBooks")
.permitAll()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/485757.html
