Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring Security JWT筛选器适用于所有请求_Java_Spring_Spring Boot_Spring Security_Jwt - Fatal编程技术网

Java Spring Security JWT筛选器适用于所有请求

Java Spring Security JWT筛选器适用于所有请求,java,spring,spring-boot,spring-security,jwt,Java,Spring,Spring Boot,Spring Security,Jwt,我正在处理一个Spring引导服务,它正在接收请求头中另一个服务中生成的JWT令牌。 我的目标是验证Spring引导应用程序中JWT令牌的有效性 经过一段时间的研究,我决定使用SpringSecurity的WebSecurity配置,并拥有某种中间件,实际上是一个过滤器 我已将筛选器配置为不应用于应用程序中定义的所有请求。但不管如何,过滤器都应用于配置为permitAll()的请求 我完全被这一切弄糊涂了,弄不清我错过了什么。最后我决定寻求帮助。请帮忙 这是我的密码 安全配置: @Configu

我正在处理一个Spring引导服务,它正在接收请求头中另一个服务中生成的JWT令牌。 我的目标是验证Spring引导应用程序中JWT令牌的有效性

经过一段时间的研究,我决定使用SpringSecurity的WebSecurity配置,并拥有某种中间件,实际上是一个过滤器

我已将筛选器配置为不应用于应用程序中定义的所有请求。但不管如何,过滤器都应用于配置为permitAll()的请求

我完全被这一切弄糊涂了,弄不清我错过了什么。最后我决定寻求帮助。请帮忙

这是我的密码

安全配置:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
JWTAuthenticationFilter jwtAuthenticationFilter;

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .csrf().disable()
        .authorizeRequests()
            .antMatchers("/").permitAll()
            .antMatchers("/req").permitAll()
            .and()
        .authorizeRequests()
            .anyRequest().authenticated()
            .and()
        // And filter other requests to check the presence of JWT in header
        .addFilterBefore(jwtAuthenticationFilter,
                BasicAuthenticationFilter.class);
}

@Override
public void configure(WebSecurity webSecurity) {
    webSecurity
        .ignoring()
            .antMatchers("/req");
    }
}
过滤器:

@Component
public class JWTAuthenticationFilter extends GenericFilterBean {
@Autowired
TokenAuthenticationService tokenAuthenticationService = new TokenAuthenticationService();
@Override
public void doFilter(ServletRequest request,
        ServletResponse response,
        FilterChain filterChain)
                throws IOException, ServletException {
    boolean authentication = tokenAuthenticationService
            .getAuthentication((HttpServletRequest)request);

    if (!authentication) {
        ((HttpServletResponse) response).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        return;
    }

    filterChain.doFilter(request,response);
    }
}
身份验证服务:

@Component
class TokenAuthenticationService {
@Value("${security.authentication.token.secret}")
private String SECRET;
@Value("${security.authentication.token.token_prefix}")
private String TOKEN_PREFIX;
@Value("${security.authentication.token.header_string}")
private String HEADER_STRING;

boolean getAuthentication(HttpServletRequest request) throws UnsupportedEncodingException {
    String token = request.getHeader(HEADER_STRING);

    if (token != null) {
        // parse the token.
        DecodedJWT jwt;
        try {
            Algorithm algorithm = Algorithm.HMAC256(SECRET);
            JWTVerifier verifier = JWT.require(algorithm)
                .build(); //Reusable verifier instance
            jwt = verifier.verify(token);

            return true;
        } catch (UnsupportedEncodingException exception){
            //UTF-8 encoding not supported
            UnsupportedEncodingException ex = exception;
            return false;
        } catch (JWTVerificationException exception){
            //Invalid signature/claims
            JWTVerificationException ex = exception;
            return false;
        }
    }
    return false;
    }
}

您需要使用ResourceServiceConfiguration类而不是WebSecurityConfig进行JWT验证。请检查此链接-


您需要使用ResourceServiceConfiguration类而不是WebSecurityConfig进行JWT验证。请检查此链接-


您编写了我已将筛选器配置为不应用于所有请求,但我无法在您的筛选器中看到此类代码。您总是对请求进行身份验证。那么,antMatchers().permitAll()和ignoring().antMatchers()不是意味着要在这些路径上跳过筛选器吗?你能给我举一个例子说明这个配置是如何应用于filter类的吗?
忽略().antMatchers()
应该可以工作,因为它排除了整个过滤器链,但是Spring Boot会自动将所有servlet过滤器添加到应用程序中。您是否在Spring Boot配置中排除了筛选器?可能与@dur重复?您是否在Spring Boot配置中排除了筛选器?不是我干的。但是我需要它吗?因为在我的筛选器类中,我想从application.properties中注入值,如果我理解正确,它会将其从组件扫描链中排除?您写道,我已将筛选器配置为不应用于所有请求,但我无法在您的筛选器中看到此类代码。您总是对请求进行身份验证。那么,antMatchers().permitAll()和ignoring().antMatchers()不是意味着要在这些路径上跳过筛选器吗?你能给我举一个例子说明这个配置是如何应用于filter类的吗?
忽略().antMatchers()
应该可以工作,因为它排除了整个过滤器链,但是Spring Boot会自动将所有servlet过滤器添加到应用程序中。您是否在Spring Boot配置中排除了筛选器?可能与@dur重复?您是否在Spring Boot配置中排除了筛选器?不是我干的。但是我需要它吗?因为在我的筛选器类中,我想从application.properties中注入值,如果我理解正确,它会将其从组件扫描链中排除?在您的示例中,我看不到筛选器和通配符请求。您使用的是@EnableResourceServer吗?这是用于JWT验证的。您好,我没有使用@EnableResourceServer。你能给我举个例子吗?我的应用程序是使用swagger codegenI生成的simple@SpringBootApplication,在您的示例中我看不到筛选器和通配符请求。您使用的是@EnableResourceServer吗?这是用于JWT验证的。您好,我没有使用@EnableResourceServer。你能给我举个例子吗?我的应用程序是使用swagger codegen生成的simple@SpringBootApplication
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;

@Configuration
//@Order(-21)
@EnableResourceServer
public class ResourceServiceConfiguration extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests().antMatchers("/**").hasAnyAuthority ("READ_PRIVILEGE","WRITE_PRIVILEGE");
    }

}