BAEL-1222: Hit the Ground Running with the Spring Security Java Configuration (#2988)
This commit is contained in:
+15
@@ -0,0 +1,15 @@
|
||||
package com.stackify.guest.springsecurity;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackages = {"com.stackify.guest.springsecurity"})
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.stackify.guest.springsecurity.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebMvcConfiguration implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
registry.addViewController("/customLogin").setViewName("customLogin");
|
||||
registry.addViewController("/loginSuccess").setViewName("index");
|
||||
}
|
||||
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.stackify.guest.springsecurity.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
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.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.provisioning.JdbcUserDetailsManager;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@EnableWebSecurity
|
||||
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
|
||||
@Bean
|
||||
public UserDetailsService jdbcUserDetailsService(DataSource dataSource) {
|
||||
JdbcUserDetailsManager manager = new JdbcUserDetailsManager();
|
||||
manager.setDataSource(dataSource);
|
||||
return manager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PasswordEncoder passwordEncoder() {
|
||||
return new BCryptPasswordEncoder();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/css/**").permitAll()
|
||||
.anyRequest().authenticated()
|
||||
.and().formLogin()
|
||||
.loginPage("/customLogin")
|
||||
.defaultSuccessUrl("/loginSuccess", true)
|
||||
.permitAll();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
INSERT INTO users VALUES ('jill', '$2a$04$qUlqAEEYF1YvrpJMosodoewgL6aO.qgHytl2k5L7kdXEWnJsFdxvq', TRUE);
|
||||
INSERT INTO authorities VALUES ('jill', 'USERS');
|
||||
@@ -0,0 +1,10 @@
|
||||
CREATE TABLE users (
|
||||
username VARCHAR(256) PRIMARY KEY,
|
||||
password VARCHAR(256),
|
||||
enabled BOOLEAN
|
||||
);
|
||||
|
||||
CREATE TABLE authorities (
|
||||
username VARCHAR(256) REFERENCES users (username),
|
||||
authority VARCHAR(256)
|
||||
);
|
||||
@@ -0,0 +1,3 @@
|
||||
.bad-login {
|
||||
color: red;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||
xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<link rel="stylesheet" href="/css/styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<form th:action="@{/customLogin}" method="post">
|
||||
<fieldset>
|
||||
<label for="username">Login:</label>
|
||||
<input id="username" name="username">
|
||||
<label for="password">Password:</label>
|
||||
<input id="password" name="password" type="password">
|
||||
</fieldset>
|
||||
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
|
||||
<input type="submit" value="Login">
|
||||
<div th:if="${param.error}" class="bad-login">Bad login or password.</div>
|
||||
<div th:if="${param.logout}">Log out successful.</div>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||
xmlns:th="http://www.thymeleaf.org">
|
||||
<p>Hello, <span th:text="${
|
||||
T(org.springframework.security.core.context.SecurityContextHolder)
|
||||
.context.authentication.principal.username}"></span>!</p>
|
||||
<form action="/logout" method="post">
|
||||
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
|
||||
<input type="submit" value="Log Out">
|
||||
</form>
|
||||
</html>
|
||||
Reference in New Issue
Block a user