Skip to content

Commit

Permalink
Merge pull request #8 from ls1intum/add-basic-auth
Browse files Browse the repository at this point in the history
Add basic auth to secure GET requests to api/telemetry
  • Loading branch information
BBesrour authored Oct 26, 2024
2 parents 34b8a1c + e158ffb commit c13eef6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@ artemis:
enabled: true
sendAdminDetails: true
destination: http://localhost:8081
```

We use basic authentication for getting the data from the telemetry service. You will need to adjust the `application.yml` of the telemetry service accordingly:
```
telemetry:
user: <user>
password: <password>
```
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ repositories {
dependencies {
implementation "org.springframework.boot:spring-boot-starter-data-jpa"
implementation "org.springframework.boot:spring-boot-starter-web"
implementation "org.springframework.boot:spring-boot-starter-security"
implementation "org.hibernate.orm:hibernate-community-dialects:${hibernate_version}"
implementation "org.apache.commons:commons-lang3"
implementation "com.zaxxer:HikariCP"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package de.tum.cit.ase.artemistelemetry.security;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

@Value("${telemetry.user}")
private String telemetryUser;

@Value("${telemetry.password}")
private String telemetryPassword;



@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable())
.authorizeHttpRequests(auth -> auth
.requestMatchers(HttpMethod.GET, "/api/telemetry/**").authenticated()
.anyRequest().permitAll()
)
.httpBasic(httpBasic -> {});

return http.build();
}

@Bean
public UserDetailsService userDetailsService() {
UserDetails user = User.withDefaultPasswordEncoder()
.username(telemetryUser)
.password(telemetryPassword)
.roles("USER")
.build();

return new InMemoryUserDetailsManager(user);
}
}

0 comments on commit c13eef6

Please sign in to comment.