java - Spring Boot:禁用状态异常代码的安全性
问题描述
我有一个安全的 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 的 rest 异常时,Spring Boot 会自动重定向到 url“/error”。但是这个url映射需要开通业务。所以我也不得不忽略这个网址的授权。
这里的解决方案是添加这个:
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");
}
}
推荐阅读
- c++ - 无法使用实例化的 json_pointer 更新 nlohmann::json 文档
- python - 如何在绘制线条时调用已绑定到 tkinter 画布上的图像的事件处理程序?
- python - 从其他文件夹打开文件的Python问题
- javascript - google apis 将视频上传到我的 youtube 频道
- python - 如何导入熊猫库?
- javascript - TypeError:this.timeout 不是函数
- wordpress - 在apache htaccess中将页面url重写为子域
- random - 智能合约 - Chainlink VRF / @openzeppelin/truffle-upgrades 兼容性
- python - pandas 检查 MultiIndex 数据帧中的一个特定索引中是否存在值
- angular - 向表头添加排序不起作用 - 带有 Angular 模板项目的 ASP.NET Core 3.1