Skip to content

Commit

Permalink
JDBC-based user store & display login error message
Browse files Browse the repository at this point in the history
  • Loading branch information
theEmperorofDaiViet committed Nov 29, 2022
1 parent cb9c028 commit 0fe0a00
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
22 changes: 13 additions & 9 deletions src/main/java/tacos/security/SecurityConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package tacos.security;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
Expand All @@ -12,19 +15,20 @@
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{

@Autowired
DataSource dataSource;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
auth.inMemoryAuthentication()
.withUser("holo")
.password(encoder.encode("thewisewolf"))
.authorities("ROLE_USER")
.and()
.withUser("mai")
.password(encoder.encode("bunnygirlsenpai"))
.authorities("ROLE_USER");
System.out.println(org.springframework.security.core.SpringSecurityCoreVersion.getVersion());
auth.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery(
"Select username, password, enabled from Users where username=?")
.authoritiesByUsernameQuery(
"select username, authority from Authorities where username=?")
.passwordEncoder(encoder);

}

Expand Down
17 changes: 17 additions & 0 deletions src/main/resources/Users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use tacocloud2;
create table Users(
username varchar(50) not null primary key,
password varchar(500) not null,
enabled bit not null
);

create table Authorities (
username varchar(50) not null,
authority varchar(50) not null,
constraint fk_authorities_users foreign key(username) references users(username)
);
create unique index ix_auth_username on authorities (username,authority);

insert into dbo.Users (username, password, enabled) values ('yoko', 'littner', 1);

insert into dbo.Authorities (username, authority) values ('yoko', 'ROLE_USER');
4 changes: 2 additions & 2 deletions src/main/resources/templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Taco Cloud</title>
<link rel="stylesheet" th:href="@{/styles.css?version=1}" />
<link rel="stylesheet" th:href="@{/styles.css}" />
</head>

<body>
<h1>Login</h1>
<img th:src="@{/images/TacoCloud.png}"/>

<div class="validationError" th:if="${error}">
<div class="validationError" th:if="${param.error}">
Unable to login. Check your username and password.
</div>

Expand Down

0 comments on commit 0fe0a00

Please sign in to comment.