java - 如何与请求过滤器共享 WebSecurityConfigurerAdapter 中的 HttpSecurity?
问题描述
我有一个 Spring Boot 应用程序,我在其中配置了 REST 安全性,其中 JWT 身份验证覆盖了
protected void configure(HttpSecurity httpSecurity) throws Exception {
我的配置类中的方法:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
// ...
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.csrf().disable()
// ...
.authorizeRequests()
// ...
.antMatchers("/api/authentication/login").permitAll()
.antMatchers("/api/assets/*/processed-data/csv").permitAll()
// ...
.antMatchers("/api/**").authenticated()
// ...
由于某些路径需要身份验证,而其他路径不需要,我想在相应的 JWT 令牌验证过滤器中配对该配置,我实际上列出了每个子路径:
@Component
public class JwtRequestFilter extends OncePerRequestFilter {
// ...
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
if(request.getRequestURI().startsWith("/api/authentication/")) {
// ...
HttpSecurity
不是 bean,所以我不能自动装配它。我能做些什么?我应该让我的WebSecurityConfig
类成为一个 bean 并添加一个getHttpSecurity()
方法吗?有没有更清洁的方法?还是根本不推荐这种方法?
解决方案
推荐阅读
- symfony - Docker:连接到数据库容器时连接超时
- javascript - 如何为字符串生成唯一但一致的 N 位哈希(小于 64 位)?
- java - 如何在一系列单选按钮之上添加一个简单的字符串?
- ios - 从 UITableView 和 Firebase 中删除单元格
- sql - Update multiple rows in SQL Server with different table
- sas - 使用 Proc sql 获取许多变量的统计信息
- dart - 颤振中的侧面径向菜单
- azure-web-app-service - 如何在 azurewebsites.net 下为 Azure App Service 应用程序添加第二个域名?
- ios - 如何从另一个类访问一个类中的值?斯威夫特 4
- dart - 为什么更新列表
ListView 中使用的那个无法更新显示?