フランク:
(トライアル&エラーをして、同様の質問のためのSO検索)数日間、これで苦労した後、私はあきらめて誘惑しています...
問題があるので、私は認証のために春のセキュリティとJWTを使用して春のブートに基づいてRESTサービスを提供しています。今、私は使用して認可の人々によって呼び出されるメソッドの一部を確保したい@PreAuthorize
-annotationを。これが理由の一つ代わりの春、私は403を期待していた404を返すメソッドを呼び出す動作するようです。
私は、この読んだことがあるSO-質問をし、そこに与えられた答えを試みたが、それは助けにはなりませんでした。私が移動してきた@EnableGlobalMethodSecurity(prePostEnabled = true)
他の場所で示唆したように、まだそれは動作しません、Applicationクラスに私のSecurityConfigurationから-annotationを。
私のセキュリティ設定は次のようになります。
@Configuration
@Profile("production")
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Value("${adDomain}")
private String adDomain;
@Value("${adUrl}")
private String adUrl;
@Value("${rootDn}")
private String rootDn;
@Value("${searchFilter}")
private String searchFilter;
private final AuthenticationManagerBuilder auth;
private final SessionRepository sessionRepository;
@Autowired
public SecurityConfiguration(AuthenticationManagerBuilder auth, SessionRepository sessionRepository) {
this.auth = auth;
this.sessionRepository = sessionRepository;
}
@Override
public void configure(WebSecurity webSecurity) throws Exception
{
webSecurity
.ignoring()
// All of Spring Security will ignore the requests
.antMatchers("/static/**", "/api/web/logout")
.antMatchers(HttpMethod.POST, "/api/web/login");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable() // Using JWT there is no need for CSRF-protection!
.authorizeRequests()
.anyRequest().authenticated()
.and()
.addFilter(new JwtAuthorizationFilter(authenticationManagerBean(), sessionRepository));
}
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
ActiveDirectoryLdapAuthenticationProvider adProvider =
new ActiveDirectoryLdapAuthenticationProvider(adDomain, adUrl, rootDn);
adProvider.setConvertSubErrorCodesToExceptions(true);
adProvider.setUseAuthenticationRequestCredentials(true);
adProvider.setSearchFilter(searchFilter);
adProvider.setUserDetailsContextMapper(new InetOrgPersonContextMapper());
auth.authenticationProvider(adProvider);
return super.authenticationManagerBean();
}
}
このようなコントローラメソッドのルックス
@RequestMapping(path = "/licenses", method = RequestMethod.GET)
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<?> getAllLicenses(@RequestParam("after") int pagenumber, @RequestParam("size") int pagesize
, @RequestParam("searchText") String searchText) {
List<LicenseDTO> result = ...
return new ResponseEntity<Object>(result, HttpStatus.OK);
}
私はかなり確実私は非常にシンプルなものをしないのですが、私はちょうど何を把握することはできません。
ところで:問題は本当の404ではないので、ライセンスを要求するユーザは、すべてが期待どおりに動作ADMINの役割を持っている場合。
ScanQR:
あなたは、次のようにセキュリティ設定でexceptionHandlingを定義する必要があります
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable() // Using JWT there is no need for CSRF-protection!
.authorizeRequests()
.anyRequest().authenticated()
.and()
.exceptionHandling().accessDeniedHandler(new AccessDeniedExceptionHandler())
.and()
.addFilter(new JwtAuthorizationFilter(authenticationManagerBean(), sessionRepository));
}
次のようにAccessDeniedExceptionHandlerクラスを定義することができ、
public class AccessDeniedExceptionHandler implements AccessDeniedHandler
{
@Override
public void handle(HttpServletRequest request, HttpServletResponse response,
AccessDeniedException ex) throws IOException, ServletException {
response.setStatus(HttpStatus.FORBIDDEN);
}
}