Home > Net >  Spring Security config ignored while running on Docker
Spring Security config ignored while running on Docker

Time:10-04

I dockerized my simple spring boot application and I started by addind a repo, check that the connection works (and it does) and a simple controller for a simple model. Also added a controller for health checks. I permit all by a configuration, but everytime i try to access it, it redirects me to localhost:8080/login. Why is that?

Also if i run this project as it is (not as a docker image), it works.

My config (i use this because WebSecurityConfigurerAdapter seems to be deprecated) :

@Configuration
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                .authorizeHttpRequests((authz) -> authz
                        .anyRequest().permitAll()
                )
                .httpBasic(withDefaults());
        return http.build();
    }
}

application.yml:

spring:
  datasource:
    url: ${SPRING_DATASOURCE_URL}
    username: ${SPRING_DATASOURCE_USERNAME}
    password: ${SPRING_DATASOURCE_PASSWORD}
  jpa:
    hibernate:
      ddl-auto: none

docker-compose.yml

version: '2'

services:
  app:
    image: spring-backend
    ports:
      - "8080:8080"
    depends_on:
      - db
    environment:
      - SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/local_db
      - SPRING_DATASOURCE_USERNAME=root
      - SPRING_DATASOURCE_PASSWORD=postgres
      - SPRING_JPA_HIBERNATE_DDL_AUTO=none
      - JAR_FILE=./build/libs/*.jar

  db:
    image: 'postgres:13.1-alpine'
    container_name: local_db
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=root
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=local_db

Dockerfile:

FROM openjdk:11-jre
ARG JAR_FILE=./build/libs/*.jar
COPY ${JAR_FILE} app.jar
EXPOSE 8080
CMD ["java", "-jar", "app.jar"]

Thanks!

CodePudding user response:

You should disable httpBasic.

httpBasic.withDefaults()

activates spring security.

see:

HttpBasicConfigurer

  • Related