BAEL-2762 Fix tests in spring-boot-security module
This commit is contained in:
parent
7b5d3a20e1
commit
284542701f
@ -5,6 +5,8 @@ import org.springframework.security.config.annotation.authentication.builders.Au
|
|||||||
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.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
|
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
@ -12,14 +14,17 @@ public class BasicAuthConfiguration extends WebSecurityConfigurerAdapter {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||||
|
PasswordEncoder encoder =
|
||||||
|
PasswordEncoderFactories.createDelegatingPasswordEncoder();
|
||||||
|
|
||||||
auth
|
auth
|
||||||
.inMemoryAuthentication()
|
.inMemoryAuthentication()
|
||||||
.withUser("user")
|
.withUser("user")
|
||||||
.password("password")
|
.password(encoder.encode("password"))
|
||||||
.roles("USER")
|
.roles("USER")
|
||||||
.and()
|
.and()
|
||||||
.withUser("admin")
|
.withUser("admin")
|
||||||
.password("admin")
|
.password("{noop}admin")
|
||||||
.roles("USER", "ADMIN");
|
.roles("USER", "ADMIN");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,13 +25,13 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
|
|||||||
clients
|
clients
|
||||||
.inMemory()
|
.inMemory()
|
||||||
.withClient("baeldung")
|
.withClient("baeldung")
|
||||||
.secret("baeldung")
|
.secret("{noop}baeldung")
|
||||||
.authorizedGrantTypes("client_credentials", "password", "authorization_code")
|
.authorizedGrantTypes("client_credentials", "password", "authorization_code")
|
||||||
.scopes("openid", "read")
|
.scopes("openid", "read")
|
||||||
.autoApprove(true)
|
.autoApprove(true)
|
||||||
.and()
|
.and()
|
||||||
.withClient("baeldung-admin")
|
.withClient("baeldung-admin")
|
||||||
.secret("baeldung")
|
.secret("{noop}baeldung")
|
||||||
.authorizedGrantTypes("authorization_code", "client_credentials", "refresh_token")
|
.authorizedGrantTypes("authorization_code", "client_credentials", "refresh_token")
|
||||||
.scopes("read", "write")
|
.scopes("read", "write")
|
||||||
.autoApprove(true);
|
.autoApprove(true);
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.springbootsecurity.oauth2server.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public AuthenticationManager customAuthenticationManager() throws Exception {
|
||||||
|
return authenticationManager();
|
||||||
|
}
|
||||||
|
}
|
@ -14,7 +14,7 @@ public class SpringBootSecurityTagLibsConfig extends WebSecurityConfigurerAdapte
|
|||||||
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||||
auth.inMemoryAuthentication()
|
auth.inMemoryAuthentication()
|
||||||
.withUser("testUser")
|
.withUser("testUser")
|
||||||
.password("password")
|
.password("{noop}password")
|
||||||
.roles("ADMIN");
|
.roles("ADMIN");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.baeldung.springbootsecurity.basic_auth;
|
package com.baeldung.springbootsecurity.basic_auth;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
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;
|
||||||
@ -49,8 +50,6 @@ public class BasicAuthConfigurationIntegrationTest {
|
|||||||
ResponseEntity<String> response = restTemplate.getForEntity(base.toString(), String.class);
|
ResponseEntity<String> response = restTemplate.getForEntity(base.toString(), String.class);
|
||||||
|
|
||||||
assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
|
assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
|
||||||
assertTrue(response
|
Assert.assertNull(response.getBody());
|
||||||
.getBody()
|
|
||||||
.contains("Unauthorized"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user