JAVA-14889 Update spring-security-web-angular module under spring-security-modules to remove usage of deprecated WebSecurityConfigurerAdapter (#12960)

This commit is contained in:
anuragkumawat 2022-11-06 10:53:53 +05:30 committed by GitHub
parent 548c69164f
commit ce4b255177
4 changed files with 30 additions and 41 deletions

View File

@ -10,8 +10,9 @@
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>spring-security-modules</artifactId> <artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
</parent> </parent>
<modules> <modules>

View File

@ -14,18 +14,6 @@
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
</parent> </parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies> <dependencies>
<!--<dependency> --> <!--<dependency> -->
<!--<groupId>org.springframework.boot</groupId> --> <!--<groupId>org.springframework.boot</groupId> -->
@ -60,8 +48,4 @@
</plugins> </plugins>
</build> </build>
<properties>
<spring-boot.version>1.5.9.RELEASE</spring-boot.version>
</properties>
</project> </project>

View File

@ -1,34 +1,41 @@
package com.baeldung.springbootsecurityrest.basicauth.config; package com.baeldung.springbootsecurityrest.basicauth.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
@Configuration @Configuration
@EnableWebSecurity @EnableWebSecurity
public class BasicAuthConfiguration extends WebSecurityConfigurerAdapter { public class BasicAuthConfiguration {
@Override @Bean
protected void configure(AuthenticationManagerBuilder auth) throws Exception { public InMemoryUserDetailsManager userDetailsService() {
auth UserDetails user = User.withUsername("user")
.inMemoryAuthentication() .password("{noop}password")
.withUser("user") .roles("USER")
.password("password") .build();
.roles("USER"); return new InMemoryUserDetailsManager(user);
} }
@Override @Bean
protected void configure(HttpSecurity http) throws Exception { public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.csrf().disable() http.csrf()
.authorizeRequests() .disable()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll() .authorizeRequests()
.antMatchers("/login").permitAll() .antMatchers(HttpMethod.OPTIONS, "/**")
.anyRequest() .permitAll()
.authenticated() .antMatchers("/login")
.and() .permitAll()
.httpBasic(); .anyRequest()
.authenticated()
.and()
.httpBasic();
return http.build();
} }
} }

View File

@ -11,7 +11,7 @@ import java.net.URL;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -80,8 +80,5 @@ public class BasicAuthConfigurationIntegrationTest {
ResponseEntity<String> response = restTemplate.getForEntity(base.toString()+"/user", String.class); ResponseEntity<String> response = restTemplate.getForEntity(base.toString()+"/user", String.class);
assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
assertTrue(response
.getBody()
.contains("Unauthorized"));
} }
} }