[英]Mix HttpBasic and FormLogin in Spring Security with Spring-boot-starter
我使用具有彈簧安全性的spring-boot-starter 0.5.0.M6構建包含以下內容的應用程序:
我的第一次嘗試是:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/resources/**").permitAll()
.antMatchers("/admin/**").hasRole("ADMIN")
.and()
.formLogin()
.defaultSuccessUrl("/admin/home")
.loginPage("/login")
.permitAll()
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout", "GET"))
.permitAll();
http
.authorizeRequests()
.antMatchers("/api/**").hasRole("API")
.and()
.httpBasic();
}
使用這種方法:
然后我嘗試使用https://github.com/spring-projects/spring-security-javaconfig/blob/master/samples-web.md#sample-multi-http-web-configuration
的解決方案,但我只能保護/api/**
或/admin/**
但不能同時保護二者,取決於我使用@Order
注釋的哪個。
請幫我一下。
非常感謝
對於您的api部分,請使用以下內容。 請注意,第一個螞蟻匹配器限制了此安全配置過濾的內容的范圍。 那是我起初從您的參考文獻中不了解的部分。
@Configuration
@Order(1)
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
// the ant matcher is what limits the scope of this configuration.
http.antMatcher("/api/**").authorizeRequests()
.antMatchers("/api/**").authenticated()
.and().httpBasic().realmName("Sourcing API");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.