EP.2 ## JDBC authentication with Spring Security แบบเข้าใจง่าย
มาต่อกันจาก ep ที่แล้วนะครับ ก่อนหน้านี้เรา add user เข้าไปใน configuration เลย
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource)
.withDefaultSchema()
.withUser(User.withUsername("user").password("password").roles("USER"))
.withUser(User.withUsername("admin").password("password").roles("ADMIN"));
}
ต่อไปเราจะสร้าง user ใน Database แล้วเราจะทำการ authentication จาก user ที่อยู่ใน Database นั้น ตามเอกสารนี้นะครับ https://docs.spring.io/spring-security/site/docs/5.1.1.RELEASE/reference/html/appendix.html
สร้าง DDL ใน schema.sql ใน resources
create table users(
username varchar_ignorecase(50) not null primary key,
password varchar_ignorecase(50) not null,
enabled boolean not null
);
create table authorities (
username varchar_ignorecase(50) not null,
authority varchar_ignorecase(50) not null,
constraint fk_authorities_users foreign key(username) references users(username)
);
create unique index ix_auth_username on authorities (username,authority);
ต่อไปสร้าง data.sql เพื่อสร้าง user & authority ใน resources
INSERT INTO users (username, password, enabled)
values ('user', 'pass', true);
INSERT INTO users (username, password, enabled)
values ('admin', 'pass', true);
INSERT INTO authorities (username, authority)
values ('user', 'ROLE_USER');
INSERT INTO authorities (username, authority)
values ('admin', 'ROLE_ADMIN');
แก้ SecurityConfiguration.java ให้เป็นแบบนี้
package com.zengcode.springsecurityjdbc.configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.User;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import javax.sql.DataSource;
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin").hasRole("ADMIN")
.antMatchers("/user").hasAnyRole("ADMIN", "USER")
.antMatchers("/").permitAll()
.and().formLogin();
}
@Bean
public PasswordEncoder getPasswordEncoder() {
return NoOpPasswordEncoder.getInstance();
}
}
หรือว่า
auth.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery("select username,password,enabled
from users where username = ?")
.authoritiesByUsernameQuery("select username,authority from
authorities where username = ?");
ก็จะสามารถสร้าง table name อื่นๆ ได้ จากนั้นก็รัน
ลองเข้า http://localhost:8080/admin ดู มันจะ redirect ไปหน้า login เนอะ
ป้อน admin pass ก็จะเข้าได้แล้ว ก็เหมือนกับ EP ที่แล้วทุกประการเนอะ
หรือจะลองเข้า http://localhost:8080/users ดูก็เข้าได้ เย้
ต่อไป http://localhost:8080/logout แล้ว login เป็น user ดู ป้อน user pass
ลองเข้า http://localhost:8080/users
ลองเข้า http://localhost:8080/admin
ก็เข้าไม่ได้เพราะว่าหน้านี้ให้แต่ admin เข้าเท่านั้น
จบเนอะ เคลียร์ปะ