· 8 years ago · Aug 26, 2018, 09:50 PM
1<parent>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-parent</artifactId>
4 <version>2.0.4.RELEASE</version>
5</parent>
6
7<properties>
8 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
9 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
10 <java.version>1.8</java.version>
11</properties>
12
13
14<dependencies>
15
16<!-- Spring Boot dependencies -->
17 <dependency>
18 <groupId>org.springframework.boot</groupId>
19 <artifactId>spring-boot-starter-web</artifactId>
20 </dependency>
21
22 <dependency>
23 <groupId>org.springframework.boot</groupId>
24 <artifactId>spring-boot-starter-test</artifactId>
25 <scope>test</scope>
26 </dependency>
27
28 <dependency>
29 <groupId>org.springframework.boot</groupId>
30 <artifactId>spring-boot-devtools</artifactId>
31 <scope>runtime</scope>
32 </dependency>
33
34 <dependency>
35 <groupId>org.springframework.boot</groupId>
36 <artifactId>spring-boot-starter-security</artifactId>
37 </dependency>
38
39 <dependency>
40 <groupId>org.springframework.boot</groupId>
41 <artifactId>spring-boot-starter-thymeleaf</artifactId>
42 </dependency>
43
44 <dependency>
45 <groupId>org.thymeleaf.extras</groupId>
46 <artifactId>thymeleaf-extras-springsecurity4</artifactId>
47 </dependency>
48
49<!-- Persistence dependencies -->
50 <dependency>
51 <groupId>org.springframework.boot</groupId>
52 <artifactId>spring-boot-starter-data-jpa</artifactId>
53 </dependency>
54
55 <dependency>
56 <groupId>org.hibernate</groupId>
57 <artifactId>hibernate-core</artifactId>
58 </dependency>
59
60 <dependency>
61 <groupId>mysql</groupId>
62 <artifactId>mysql-connector-java</artifactId>
63 <scope>runtime</scope>
64 </dependency>
65
66
67<!-- Tools dependencies -->
68 <dependency>
69 <groupId>org.projectlombok</groupId>
70 <artifactId>lombok</artifactId>
71 <version>1.18.2</version><!--$NO-MVN-MAN-VER$-->
72 <scope>provided</scope>
73 </dependency>
74
75
76</dependencies>
77
78<build>
79 <plugins>
80 <plugin>
81 <groupId>org.springframework.boot</groupId>
82 <artifactId>spring-boot-maven-plugin</artifactId>
83 </plugin>
84 </plugins>
85</build>
86
87spring.datasource.driver-class-name=com.mysql.jdbc.Driver
88spring.datasource.url = jdbc:mysql://localhost:3306/lostdb?useSSL=false&serverTimezone=UTC
89spring.datasource.username = lostuser
90spring.datasource.password = lostuser
91
92
93## Hibernate Properties
94spring.jpa.properties.hibernate.dialect =
95org.hibernate.dialect.MySQL5InnoDBDialect
96
97# Hibernate ddl auto (create, create-drop, validate, update)
98spring.jpa.hibernate.ddl-auto = update
99
100# Show SQL queries
101spring.jpa.show-sql=true
102spring.jpa.properties.hibernate.use_sql_comments=true
103spring.jpa.properties.hibernate.format_sql=true
104
105logging.level.org.hibernate.SQL=DEBUG
106logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
107
108# Hikari CP
109spring.datasource.hikari.connection-timeout=60000
110spring.datasource.hikari.minimum-idle=5
111spring.datasource.hikari.maximum-pool-size=5
112spring.datasource.hikari.idle-timeout=300000
113spring.datasource.hikari.max-lifetime=1200000
114spring.datasource.hikari.auto-commit=true
115
116@Configuration
117@EnableWebSecurity
118public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
119
120@Autowired
121private DataSource dataSource;
122
123@Override
124protected void configure(HttpSecurity http) throws Exception {
125 http
126 .authorizeRequests()
127 //Permit these requests without authentication
128 .antMatchers("/", "/login", "/signup").permitAll()
129 //Any other request must be authenticated
130 .anyRequest().authenticated()
131 .and()
132 .formLogin()
133 .loginPage("/login")
134 .loginProcessingUrl("/userAuthentication")
135 .permitAll()
136 .and()
137 .logout()
138 .permitAll();
139}
140
141
142@Autowired
143public void configureGloal(AuthenticationManagerBuilder auth) throws Exception {
144 auth.jdbcAuthentication().dataSource(dataSource).
145 usersByUsernameQuery("select username,password,enabled from users where username=?").
146 authoritiesByUsernameQuery("select username, authority from authorities where username=?");;
147 }
148
149}
150
151<body>
152<div th:replace="fragments/navbar.html"></div>
153 <div class="container">
154 <div class="starter-template">
155 <div class="alert alert-danger" role="alert" th:if="${param.error}">
156 Invalid username and password.
157 </div>
158 <div class="alert alert-success" th:if="${param.logout}">
159 You have been logged out.
160 </div>
161 <br>
162 <form th:action="@{/userAuthentication}" method="POST">
163 <div class="form-group"><label> User Name : <input type="text" name="username" class="form-control"/> </label></div>
164 <div class="form-group"><label> Password: <input type="password" name="password" class="form-control"/> </label></div>
165 <div><input type="submit" value="Log In" class="btn btn-primary"/></div>
166 </form>
167 </div>
168 </div>
169
170</body>
171
172<body>
173<div th:replace="fragments/navbar.html"></div>
174<div class="container">
175 <div class="starter-template">
176 <br>
177 <h4>
178 Logged user: <span sec:authentication="name"></span>
179 <br><br>
180 </h4>
181
182 <p>
183 <a href='${/admin}'>Admin access</a>
184
185 </p>
186
187 <form th:action="@{/logout}" method="POST">
188 <div><input type="submit" value="Log Out" class="btn btn-primary"/></div>
189 </form>
190 </div>
191</div>
192</body>
193
194DROP DATABASE IF EXISTS `lostdb`;
195CREATE DATABASE IF NOT EXISTS `lostdb`;
196use `lostdb`;
197
198DROP TABLE IF EXISTS `users`;
199CREATE TABLE `users` (
200 `username` varchar(50) NOT NULL,
201 `first_name` varchar(50) DEFAULT NULL,
202 `last_name` varchar(50) DEFAULT NULL,
203 `email` varchar(50) NOT NULL,
204 `password` varchar(50) NOT NULL,
205 `phone_number` varchar(50) DEFAULT NULL,
206 `enabled` tinyint(1) NOT NULL,
207 PRIMARY KEY (`username`)
208) ENGINE=InnoDB DEFAULT CHARSET=latin1;
209
210
211DROP TABLE IF EXISTS `authorities`;
212
213CREATE TABLE `authorities` (
214 `username` varchar(45) NOT NULL,
215 `authority` varchar(50) NOT NULL,
216 UNIQUE KEY `authorities_idx_1` (`username`,`authority`),
217 CONSTRAINT `authorities_ibfk_1` FOREIGN KEY (`username`) REFERENCES
218 `users` (`username`)
219) ENGINE=InnoDB DEFAULT CHARSET=latin1;
220
221LOCK TABLES `users` WRITE, `authorities` WRITE;
222
223Some "INSERT INTO" to have some data in there...
224UNLOCK TABLES;