我的项目中有一个非常具体的要求,与身份和身份有关。授权。我想打开3条路径/public/
,/protected/
&来自我的REST服务模块的/private/
,其行为如下:
/public/
开头的网址无需任何身份验证或授权即可访问。/private/
开头的网址。/protected/
开头的网址。为实现这一点,我通过扩展“spring resource server configurator& over the Configurator
method”构建了configure
。但不幸的是它没有用。我也尝试使用“spring web service configurator& using ignore ant url support”,但同样也无效。该配置仅适用于/private/
& /protected/
网址如下。
http.anonymous()
.disable()
.requestMatchers()
.antMatchers("/protected/**", "/private/**")
.and();
for (String protectedApiEp : configuredApis) {
http.authorizeRequests()
.antMatchers("/protected/" + protectedApiEp + "/**")
.hasAuthority(protectedApiEp);
}
http.authorizeRequests()
.antMatchers("/protected/**").denyAll()
.antMatchers("/private/**").permitAll()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
有人可以指导我如何通过以上配置启用对所有用户开放的/public/
网址吗?
答案 0 :(得分:0)
以下配置应该有效:
@EnableWebSecurity
public class WebApplicationSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(final HttpSecurity http) throws Exception {
// Allow Spring Security to authorize requests.
http
.authorizeRequests()
// Allow anyone to access URLs starting with /public/.
.antMatchers("/public/**").permitAll()
// Allow anyone with the protected role to access URLs starting with /protected/.
.antMatchers("/protected/**").hasAuthority("protected")
// Allow anyone who is authenticated successfully to access all other URLs.
.anyRequest().authenticated()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}
Here是一个示例应用程序,可显示此配置的运行情况。以mvn clean spring-boot:run
启动应用程序,然后导航到http://localhost:8080
以访问该应用程序。