Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.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引导安全性-始终为403_Java_Spring_Spring Boot_Spring Security - Fatal编程技术网

Java 无法配置spring引导安全性-始终为403

Java 无法配置spring引导安全性-始终为403,java,spring,spring-boot,spring-security,Java,Spring,Spring Boot,Spring Security,所以我必须配置spring安全性,我相信我遗漏了一些东西,因为它给了我一个403-禁止。 非常感谢任何spring专家的帮助 我把重点放在解决方案上变得简单了一点,原始代码更复杂了,但错误仍然是一样的 @EnableWebSecurity public class WebSecurityConfig { @Configuration @Order(1) public static class JWTSecurityConfigurationAdapter extends

所以我必须配置spring安全性,我相信我遗漏了一些东西,因为它给了我一个403-禁止。 非常感谢任何spring专家的帮助

我把重点放在解决方案上变得简单了一点,原始代码更复杂了,但错误仍然是一样的

@EnableWebSecurity
public class WebSecurityConfig {

    @Configuration
    @Order(1)
    public static class JWTSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .csrf()
                        .disable()
                    .sessionManagement()
                        .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                        .and()
                    .exceptionHandling()
                        .authenticationEntryPoint(WebSecurityConfig::handleException)
                        .and()
                    .addFilterAfter(new JWTAuthorizationFilter(), UsernamePasswordAuthenticationFilter.class)
                    .authorizeRequests()
                        .antMatchers("/images/**")
                        .hasAnyRole("MY_USER", "MY_ADMIN")
                        .anyRequest()
                    .authenticated();
        }
    }
}

filter类很简单,几乎不做什么:

public class JWTAuthorizationFilter extends OncePerRequestFilter {

    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response,
                                    FilterChain chain) throws IOException, ServletException {
        try {
                SecurityContextHolder.getContext()
                        .setAuthentication(new UsernamePasswordAuthenticationToken(
                                "John Doe",
                                null,
                                List.of(new SimpleGrantedAuthority("MY_USER")))
                        );
            } catch (Exception e) {
                SecurityContextHolder.clearContext();
            }

            chain.doFilter(request, response);
}
在调用REST端点之后:

GET http://localhost:8083/images/parcels/parcel1/data

它总是以spring的默认403响应结束。我不知道我错过了什么。任何帮助都会很好。

新的SimpleGrantedAuthority(“我的用户”)
是一个权威而不是角色

您应该使用
hasAnyAuthority(“我的用户”、“我的管理员”)
而不是
hasAnyRole(“我的用户”、“我的管理员”)

编辑:也可以使用角色前缀

private String defaultRolePrefix = "ROLE_";
--


如何提供您的用户名和密码?@Tashkhhisi它将是jwt令牌授权人,因此我不需要密码。我正在使用证书验证令牌。这就是为什么我没有在UsernamePasswordAuthenticationToken的构造函数中添加凭据。如果我将“null”更改为其他任何内容,则会出现相同的问题。@Kulfy
/images/parcels/{parcelId}/data
。令牌作为载体随
授权
头一起提供。但它正在发挥作用,而不是问题的一部分。如果我不在url中使用任何令牌或任何参数,这也是一个问题。即使我把函数做得像我写的那样简单,也要得到403。@Kulfy intellij idea.http谢谢!这对我来说是这样的:必须在
新的SimpleGrantedAuthority(“ROLE\u MY\u USER”)
中添加前缀。通过这种方式,我可以使用
hasAnyAuthority(String…auths)
hasAnyRole(String…roles)
方法进一步深入阅读:
 new SimpleGrantedAuthority("ROLE_MY_USER")