当前位置: 首页 > 知识库问答 >
问题:

即使在允许的URL上也访问Jwt筛选器

郗丰
2023-03-14

我正在开发一个Spring Boot应用程序,我在其中定义了要执行的过滤器,以管理令牌的获取和验证。因此,在我的Web安全类配置中,我设法做到了这一点:

@Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                // we don't need CSRF because our token is invulnerable
                .csrf().disable()

                // don't create session
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()

                .authorizeRequests()
                // .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()

                // allow anonymous resource requests
                .antMatchers(HttpMethod.GET, "/", "/*.html", "/favicon.ico", "/**/*.html", "/**/*.css", "/**/*.js").permitAll()
                .antMatchers("/auth/**").permitAll()
                .antMatchers("/places/public").permitAll()

                .anyRequest().authenticated();

        // Custom JWT based security filter
        httpSecurity.addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);

        // disable page caching
        httpSecurity.headers().cacheControl();
    }
public class JwtFilter extends OncePerRequestFilter{

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private JwtService jwtService;

    @Value("${jwt.header}")
    private String authorizationHeader;

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {

        String token = httpServletRequest.getHeader(authorizationHeader);

        String username = jwtService.getUsernameFromToken(token);

        if(username!=null && SecurityContextHolder.getContext().getAuthentication() ==null){
            UserDetails userDetails = userDetailsService.loadUserByUsername(username);

            if(jwtService.isTokenValid(token, userDetails)){
                UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
                authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));
                SecurityContextHolder.getContext().setAuthentication(authenticationToken);
            }
        }

        filterChain.doFilter(httpServletRequest, httpServletResponse);
    }

}

共有1个答案

子车心思
2023-03-14

更改您的WebSecurityConfiguration类,它将如下所示:

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity
            // we don't need CSRF because our token is invulnerable
            .csrf().disable()

            // don't create session
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()

            .authorizeRequests()                

            .anyRequest().authenticated();

    // Custom JWT based security filter
    httpSecurity.addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);

    // disable page caching
    httpSecurity.headers().cacheControl();
}

 @Override
public void configure(WebSecurity web) throws Exception {
    // Ignore spring security in these paths
    web.ignoring().antMatchers("/", "/*.html", "/favicon.ico", "/**/*.html", "/**/*.css", "/**/*.js","/auth/**","/places/public");
}

您必须从HttpSecurity configure方法中删除permitAll匹配器,并将其放入WebSecurity configure方法中。

 类似资料:
  • 我从服务器得到一个200的响应,但是access-control-allog-origin:null。 所以Chrome正在抱怨上面的错误。 尝试在控制器中手动设置标题,但现在我可以看到两个标题具有相同的名称。Chrome再次抱怨多个标题同名。

  • 问题内容: 我看到以下错误: 使用此代码: 是什么原因引起的,如何解决? 问题答案: 在当前域之外发出ajax请求时,Javascript是受限制的。 例1:您的域名为example.com,并且您想向test.com提出请求=>您不能。 例2:您的域名是example.com,并且您想向inner.example.com发送请求,但是您不能。 例3:您的域名为example.com:80,并且您

  • 我在AngularJS中创建了一个应用程序,并尝试调用Laravel API: MyApp(AngularJS):http://localhost:8080/ API(Laravel样板文件):http://localhost:8000/ api_routes.php:

  • 我正在使用Microsoft扩展访问Azure DevOps中的密钥库,从密钥库中获取秘密。我得到了这个错误消息,它似乎说我需要允许Azure Devops代理访问KeyVault的权限。“无法获取托管服务主体的访问令牌。请为虚拟机”https://aka.ms/azure-MSI-docs“配置托管服务标识(MSI)。状态代码:400,状态消息:错误请求” 我在一个单独的测试订阅中运行这个功能,

  • 我在从node.js服务器获取数据时遇到问题。 客户端是: 在服务器端,我还设置了标题: 但我犯了个错误 "XMLHttpRequest无法加载http://localhost:3003/get_testlines.对预试请求的响应没有通过权限改造检查:请求的资源上没有'Access-Control-Prover-Origin'标头。因此,来源http://localhost:3000不允许进入。

  • 我已创建IAM策略并分配给IAM用户。 请查找保险单 我未选中s3 bucket testingbucket00的“阻止新公共bucket策略”。 我尝试登录aws控制台使用IAM用户列出所有桶,但显示"访问为错误"。 我想给IAM用户分配一个桶,请帮忙。