parent
8a1279315e
commit
e39fafcc33
|
|
@ -23,10 +23,18 @@
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-security</artifactId>
|
<artifactId>spring-boot-starter-security</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
<artifactId>httpclient</artifactId>
|
<artifactId>httpclient</artifactId>
|
||||||
|
|
|
||||||
|
|
@ -19,13 +19,19 @@ package sample.ui.secure;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.security.SecurityProperties;
|
import org.springframework.boot.autoconfigure.security.SecurityProperties;
|
||||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.DependsOn;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||||
|
import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;
|
||||||
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.WebSecurityConfigurerAdapter;
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
|
|
@ -65,6 +71,12 @@ public class SampleWebSecureApplication extends WebMvcConfigurerAdapter {
|
||||||
return new ApplicationSecurity();
|
return new ApplicationSecurity();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@DependsOn("dataSourceAutoConfigurationInitializer")
|
||||||
|
public AuthenticationSecurity authenticationSecurity() {
|
||||||
|
return new AuthenticationSecurity();
|
||||||
|
}
|
||||||
|
|
||||||
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
|
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
|
||||||
protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {
|
protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
|
|
@ -78,4 +90,20 @@ public class SampleWebSecureApplication extends WebMvcConfigurerAdapter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Order(Ordered.HIGHEST_PRECEDENCE + 10)
|
||||||
|
protected static class AuthenticationSecurity extends GlobalAuthenticationConfigurerAdapter {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DataSource dataSource;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(AuthenticationManagerBuilder auth) throws Exception {
|
||||||
|
// @formatter:off
|
||||||
|
auth.jdbcAuthentication().dataSource(dataSource).withUser("admin").password("admin")
|
||||||
|
.roles("ADMIN", "USER").and().withUser("user").password("user")
|
||||||
|
.roles("USER");
|
||||||
|
// @formatter:on
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
create table users (
|
||||||
|
username varchar(256),
|
||||||
|
password varchar(256),
|
||||||
|
enabled boolean
|
||||||
|
);
|
||||||
|
|
||||||
|
create table authorities (
|
||||||
|
username varchar(256),
|
||||||
|
authority varchar(256)
|
||||||
|
);
|
||||||
|
|
@ -88,7 +88,7 @@ public class SampleSecureApplicationTests {
|
||||||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||||
MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
|
MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
|
||||||
form.set("username", "user");
|
form.set("username", "user");
|
||||||
form.set("password", "password");
|
form.set("password", "user");
|
||||||
ResponseEntity<String> entity = new TestRestTemplate().exchange(
|
ResponseEntity<String> entity = new TestRestTemplate().exchange(
|
||||||
"http://localhost:" + this.port + "/login", HttpMethod.POST,
|
"http://localhost:" + this.port + "/login", HttpMethod.POST,
|
||||||
new HttpEntity<MultiValueMap<String, String>>(form, headers),
|
new HttpEntity<MultiValueMap<String, String>>(form, headers),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue