簡體   English   中英

Spring Boot:禁用狀態異常代碼的安全性

[英]Spring Boot: disable security for status exception code

我有一個安全的Spring Boot應用程序。 我已刪除此“/ login”網址的身份驗證。

我的安全配置

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    private final JwtFilter jwtFilter;

    @Autowired
    public SecurityConfiguration(JwtFilter jwtFilter) {
        this.jwtFilter = jwtFilter;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .anonymous().and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeRequests()
                .anyRequest().authenticated().and()
                .apply(new JwtConfigurerAdapter(jwtFilter)).and()
                .exceptionHandling().authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED));
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/v2/api-docs");
        web.ignoring().antMatchers("/login");
    }
}

我的NotFound異常

@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class NotFound extends RuntimeException {
    public NotFound(String message) {
        super(message);
    }
}

我的休息控制器有登錄URL和異常返回值

@RestController
public class LoginController implements LoginService {
    @Override
    @GetMapping(value = "/login", produces = MediaType.APPLICATION_JSON_VALUE)
    public UserInfo loginUsingJson(String username, String password) {
        return findUser(username, password)).orElseThrow(() -> new NotFound("There does not exist any user by those credentials"));
    }
}

好的,這是我的問題。 當我在“/ login”上調用GET並且UserInfo存在時,它將以JSON的形式返回用戶。 這是因為web.ignoring().antMatchers("/login"); ,但如果用戶存在,則與HTTP錯誤代碼404的異常NOTFOUND,將不顯示。 它現在返回錯誤代碼401 Not Authorized。

我猜它有一些與HttpSecurity有關的東西,我必須添加一些異常或其他東西才能返回異常代碼。 但是,在HttpSecurity的授權中,我在哪里允許忽略異常處理?

我找到了答案,並希望在同樣的情況下幫助其他人。

我的問題是,當使用錯誤代碼404 NotFound返回一個休息異常時,Spring Boot會自動重定向到url“/ error”。 但是這個網址映射需要為業務開放。 所以我不得不忽略這個網址的授權。

這里的解決方案是添加:

web.ignoring().antMatchers("/error");

這是改變的類:

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    private final JwtFilter jwtFilter;

    @Autowired
    public SecurityConfiguration(JwtFilter jwtFilter) {
        this.jwtFilter = jwtFilter;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .anonymous().and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeRequests()
                .anyRequest().authenticated().and()
                .apply(new JwtConfigurerAdapter(jwtFilter)).and()
                .exceptionHandling().authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED));
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/v2/api-docs");
        web.ignoring().antMatchers("/login");
        web.ignoring().antMatchers("/error");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM