【问题标题】:How to fix "403 - Forbidden - Access Denied" when requesting ressource in Spring Cloud Microservice?在 Spring Cloud 微服务中请求资源时如何修复“403 - Forbidden - Access Denied”?
【发布时间】:2019-10-20 05:11:09
【问题描述】:

我使用 jwt 保护了我的身份验证服务,当我在标头中使用 jwt 请求资源时,一切正常。在我实现了 Eureka Service Discovery 和 Zuul Gateway 并尝试请求安全资源后,我得到以下响应:

{
"timestamp": "2019-06-04T15:28:31.690+0000",
"status": 403,
"error": "Forbidden",
"message": "Access Denied",
"path": "/user"
}

因此,仅当我通过网关发送请求时才会出现此消息。也可以通过网关获取不安全的资源,只是安全资源有问题。

身份验证服务中的安全配置:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .csrf()
            .disable()
            .cors()
            .and()
            .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .authorizeRequests()
            .antMatchers("/signup").permitAll()
            .antMatchers("/login").permitAll()
            .antMatchers("/confirm-account").permitAll()
            .antMatchers("/resendMail").permitAll()
            .antMatchers("/validate/user").permitAll()
            .antMatchers("/reset/password").permitAll()
            .antMatchers("/reset-password").permitAll()
            .antMatchers("/new/password").permitAll()
            .anyRequest().authenticated()
            .and()
            .apply(new JWTConfigurer(this.tokenProvider));
}


application.properties in auth-service

spring.datasource.url=jdbc:postgresql://localhost:5432/gamificationdb
spring.datasource.username = postgres
spring.datasource.password = root
spring.jpa.properties.hibernate.dialect =       org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.main.allow-bean-definition-overriding=true
eureka.client.serviceUrl.defaultZone= http://localhost:8080/eureka/

【问题讨论】:

  • 你有什么解决办法吗?因为我遇到了同样的问题。

标签: spring security spring-security microservices gateway


【解决方案1】:

我遇到了同样的问题。我已经使用下面的课程解决了这个问题。

创建扩展 ZuulFilter 类的过滤器类。

public class AccessFilter extends ZuulFilter {

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext requestContext = RequestContext.getCurrentContext();
        String request = requestContext.getRequest().getHeader("Authorization");
        if(request == null) {
            requestContext.setSendZuulResponse(false);
            requestContext.setResponseStatusCode(HttpStatus.SC_UNAUTHORIZED);
            return null;
        }
        return null;
    }
}

然后在安全配置类中从AccessFilter 创建一个bean。

@Bean
    public AccessFilter accessFilter() {
        return new AccessFilter();
    }

【讨论】:

    猜你喜欢
    • 2020-07-10
    • 1970-01-01
    • 2018-10-27
    • 2018-04-15
    • 2020-09-01
    • 2019-02-20
    • 2020-01-05
    • 2020-08-16
    • 1970-01-01
    相关资源
    最近更新 更多