Spring security Spring boot具有多个JWT的多个路由
我需要为我的端点实现spring JWT安全性。。我有两条路线-1条是内部路线,2条是外部路线。我试图添加下面的代码,但我的两个筛选器都在为任何请求执行。。 我可以根据url在过滤器中添加逻辑。。但我觉得这不是正确的方法。请让我知道什么是正确的方法以及如何解决Spring security Spring boot具有多个JWT的多个路由,spring-security,spring-security-oauth2,Spring Security,Spring Security Oauth2,我需要为我的端点实现spring JWT安全性。。我有两条路线-1条是内部路线,2条是外部路线。我试图添加下面的代码,但我的两个筛选器都在为任何请求执行。。 我可以根据url在过滤器中添加逻辑。。但我觉得这不是正确的方法。请让我知道什么是正确的方法以及如何解决 http .csrf().disable() .authorizeRequests() .antMatchers("/internal/**") .authenticated() .and()
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/internal/**")
.authenticated()
.and()
.addFilterBefore(jwtAuthenticationInternalFilter(), BasicAuthenticationFilter.class)
.authorizeRequests()
.antMatchers("/external/**")
.authenticated()
.and()
.addFilterBefore(jwtAuthenticationExternalFilter(), BasicAuthenticationFilter.class);
public class ExternalAuthenticationFilter extends OncePerRequestFilter {
@Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
System.out.println("Its hitting here - External");//GET THE Information and build Authentication object..
// SecurityContextHolder.getContext().setAuthentication(token);
filterChain.doFilter(request, response);
}
}
public class InternalAuthenticationFilter extends OncePerRequestFilter {
@Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
System.out.println("Its hitting here - Internal");//GET THE Information and build Authentication object..
// SecurityContextHolder.getContext().setAuthentication(token);
filterChain.doFilter(request, response);
}
}
内部和外部代码都在为任何请求执行。
样品请求
/内部/abc,/外部/xyz。。在这两种情况下,都将调用两个筛选器
请建议您可以将您的安全设置分为两个不同的配置类,并用注释标记它们,例如
@Order(1)
和@Order(2)
注释。一个配置将处理/internal
端点,另一个配置将处理/external
。在configure(HttpSecurity http)
方法中,首先指定要配置的端点,然后应用设置
参见下面的一个配置示例,第二个配置将是异常的:
@EnableWebSecurity
@Order(1)
public class ExternalEndpointsSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/internal/**")
.authorizeRequests()
.authenticated()
.and()
.addFilterBefore(jwtAuthenticationInternalFilter(), BasicAuthenticationFilter.class)
}
}
您可以将安全设置分为两个不同的配置类,并用注释标记它们,例如
@Order(1)
和@Order(2)
注释。一个配置将处理/internal
端点,另一个配置将处理/external
。在configure(HttpSecurity http)
方法中,首先指定要配置的端点,然后应用设置
参见下面的一个配置示例,第二个配置将是异常的:
@EnableWebSecurity
@Order(1)
public class ExternalEndpointsSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/internal/**")
.authorizeRequests()
.authenticated()
.and()
.addFilterBefore(jwtAuthenticationInternalFilter(), BasicAuthenticationFilter.class)
}
}
这正是我在下面的链接中找到的-1.反对票接受。。我回答了自己的问题,但有人删除了我在下面的链接中找到的内容-1.反对票接受。。我自己回答,但有人把它删掉了