-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from ls1intum/add-basic-auth
Add basic auth to secure GET requests to api/telemetry
- Loading branch information
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/de/tum/cit/ase/artemistelemetry/security/SecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |