2

I secured my auth-service with a jwt and when i request a ressource with the jwt in the header everything works fine. After I implemented a Eureka Service Discovery and a Zuul Gateway and try to request a secured ressource I get the following response:

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

So this meesage only occurs when I send the request through the Gateway. It is also possible to get unsecured ressources through the gatway, there is only a problem with secured ressources.

Security Configuration in Auth Service:

@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/
softwareUser
  • 398
  • 2
  • 6
  • 21

1 Answers1

0

I had a same problem. I've solve this using below class.

Create filter class which extending ZuulFilter class.

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;
    }
}

Then create a bean from AccessFilter in Security Configuration class.

@Bean
    public AccessFilter accessFilter() {
        return new AccessFilter();
    }
Lakshitha Gihan
  • 303
  • 1
  • 19