私はREST APIを持つSpringアプリケーションを持っています。APIルートに関するSpring Web Securityの設定
WebSecurityConfigには、ルートへのアクセスの設定があります。
これらは私の主な経路である:
- /API /認証/ログイン - ログイン目的
- /API /管理/ ** - 唯一の役割 "ADMIN"
- /APIを使用した管理者のアクセス/ ** - 認証されたユーザーのアクセス
私は2つの最後のものに問題があります。私のコードの変更に応じて、私は"/ api/admin/**"ルートを誰もが誰でもアクセスできるようにしています(さらにADMIN)。 "/ api/common/**"(最終的には正常に動作しています)に変更する必要がありますか?
はここに私のコードの一部です:上記のコードで
@Override
protected void configure(HttpSecurity http) throws Exception {
AjaxLoginFilter ajaxLoginFilter = ajaxLoginFilter();
http
.cors()
.and()
.csrf().disable()
.exceptionHandling()
.authenticationEntryPoint(this.authenticationEntryPoint)
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/api/auth/login").permitAll()
.and()
.authorizeRequests()
.antMatchers("/api/**")
.authenticated()
.and()
.addFilterBefore(ajaxLoginFilter, UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(jwtTokenAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
.authorizeRequests()
.antMatchers("/api/admin/**")
.hasRole("ADMIN")
.and()
.addFilterBefore(ajaxLoginFilter, UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(jwtTokenAuthenticationFilterAdmin(), UsernamePasswordAuthenticationFilter.class);
}
protected JwtTokenAuthenticationFilter jwtTokenAuthenticationFilter() throws Exception {
List<String> pathsToSkip = Arrays.asList("/api/auth/login", "/api/admin/**");
SkipPathRequestMatcher matcher = new SkipPathRequestMatcher(pathsToSkip, "/api/**");
JwtTokenAuthenticationFilter filter
= new JwtTokenAuthenticationFilter(failureHandler, tokenExtractor, matcher);
filter.setAuthenticationManager(this.authenticationManager);
return filter;
}
protected JwtTokenAuthenticationFilter jwtTokenAuthenticationFilterAdmin() throws Exception {
List<String> pathsToSkip = Arrays.asList("/api/auth/login", "/api/**");
SkipPathRequestMatcher matcher = new SkipPathRequestMatcher(pathsToSkip, "/api/admin/**");
JwtTokenAuthenticationFilter filter
= new JwtTokenAuthenticationFilter(failureHandler, tokenExtractor, matcher);
filter.setAuthenticationManager(this.authenticationManager);
return filter;
}
"/ API /管理/ **"は、誰にでもアクセスすることはできません。私は管理者にフィルタを用いる方法jwtTokenAuthenticationFilterAdmin()で「/ API/**」からpathsToSkipリストを削除した場合、それは誰にでもアクセス可能です。
そこで質問です - 私はそれを修正することができますか、私はちょうどから任意の認証されたユーザーがアクセスできるルートを変更する必要があります「/ API/**」に「/ API /共通/ **」?
'.authorizeRequests()'を複数回呼び出すことはできません。 –
@boristheSpiderどうすればいいですか? – user3626048
[チュートリアル](https://spring.io/guides/gs/securing-web/)を読むことをお勧めします。デフォルトの 'SecurityMetadataSource'がどのように動作するかを説明することは、このサイトの範囲外です。 –