๋ก๊ทธ์ธ
์ด์ ํ์ด์ง
์ด์ ํ์ด์ง ๋ด์ฉ์ ์ด์ด์ ์งํํฉ๋๋ค.
Code
Preview
1.
๋ฉ์ธ ํ๋ฉด
2.
๋ก๊ทธ์ธ ํ๋ฉด
์์ ํ๋ก์ธ์ค
1.
ํ๋ก์ ํธ ์์ฑ
2.
์คํ๋ง ์ํ๋ฆฌํฐ ์ค์
3.
์์ฒญ ๊ฒฝ๋ก ๋งคํ
Preview
1.
๋ฉ์ธ ํ๋ฉด
2.
๋ก๊ทธ์ธ ํ๋ฉด
3.
๋ฉ์ธ ํ๋ฉด (๋ก๊ทธ์ธ ์๋ฃ)
๋ฉ์ธ ํ๋ฉด
๋ก๊ทธ์ธ ํ๋ฉด
๋ฉ์ธ ํ๋ฉด (๋ก๊ทธ์ธ ์๋ฃ)
์์ ํ๋ก์ธ์ค
1.
ํ๋ก์ ํธ ์์ฑ
โข
build.gradle
โข
spring boot version : 2.x.x
โข
spring security version : 5.x.x
โข
์์กด์ฑ ์ค์
โฆ
Spring Web
โฆ
Spring Boot DevTools
โฆ
Spring Security
โฆ
Lombok
โฆ
Thymeleaf
โฆ
MySQL Driver
โฆ
Mybatis Framework
2.
์คํ๋ง ์ํ๋ฆฌํฐ ์ค์
โข
~/config/SecurityConfig.java
ํ๋ก์ ํธ ์์ฑ
build.gradle
spring boot 2.x.x
spring security 5.x.x
plugins {
id 'java'
id 'war'
id 'org.springframework.boot' version '2.7.17'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}
group = 'com.aloha'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '17'
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.3.1'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter-test:2.3.1'
testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
tasks.named('test') {
useJUnitPlatform()
}
Java
๋ณต์ฌ
์คํ๋ง ์ํ๋ฆฌํฐ ์ค์
~/config/SecurityConfig.java
UserDetailsService ๋น ๋ฑ๋ก
UserDetailsService ๋น์ ๋ฑ๋กํ์ฌ, ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ๋ถ๋ฌ์ฌ ์ ์๋ SQL ์ฟผ๋ฆฌ๋ฅผ ์ค์ ํด์ฃผ์ด ์ธ์ฆ ์ฒ๋ฆฌ๋ฅผ ๊ตฌํํ๋ค.
โข
์ฌ์ฉ์ ์ธ์ฆ ์ฟผ๋ฆฌ
โข
์ฌ์ฉ์ ๊ถํ ์ฟผ๋ฆฌ
@Bean
public UserDetailsService userDetailsService() {
JdbcUserDetailsManager userDetailsManager
= new JdbcUserDetailsManager(dataSource);
// ์ฌ์ฉ์ ์ธ์ฆ ์ฟผ๋ฆฌ
String sql1 = " SELECT username, password, enabled "
+ " FROM user "
+ " WHERE username = ? "
;
// ์ฌ์ฉ์ ๊ถํ ์ฟผ๋ฆฌ
String sql2 = " SELECT username, auth "
+ " FROM user_auth "
+ " WHERE username = ? "
;
userDetailsManager.setUsersByUsernameQuery(sql1);
userDetailsManager.setAuthoritiesByUsernameQuery(sql2);
return userDetailsManager;
}
Java
๋ณต์ฌ
SecurityConfig.java
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Autowired
private DataSource dataSource;
// ์คํ๋ง ์ํ๋ฆฌํฐ ์ค์ ๋ฉ์๋
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
// โ
์ธ๊ฐ ์ค์
http.authorizeRequests(requests -> requests
.antMatchers("/**").permitAll()
.anyRequest().permitAll()
);
// ๐ ํผ ๋ก๊ทธ์ธ ์ค์
http.formLogin(withDefaults());
return http.build();
}
/**
* ๐ฎโโ๏ธ๐ ์ฌ์ฉ์ ์ธ์ฆ ๊ด๋ฆฌ ๋น ๋ฑ๋ก ๋ฉ์๋
* JDBC ์ธ์ฆ ๋ฐฉ์
* โ
๋ฐ์ดํฐ ์์ค (URL, ID, PW) - application.properties
* โ
SQL ์ฟผ๋ฆฌ ๋ฑ๋ก
* โญ ์ฌ์ฉ์ ์ธ์ฆ ์ฟผ๋ฆฌ
* โญ ์ฌ์ฉ์ ๊ถํ ์ฟผ๋ฆฌ
* @return
*/
@Bean
public UserDetailsService userDetailsService() {
JdbcUserDetailsManager userDetailsManager
= new JdbcUserDetailsManager(dataSource);
// ์ฌ์ฉ์ ์ธ์ฆ ์ฟผ๋ฆฌ
String sql1 = " SELECT username, password, enabled "
+ " FROM user "
+ " WHERE username = ? "
;
// ์ฌ์ฉ์ ๊ถํ ์ฟผ๋ฆฌ
String sql2 = " SELECT username, auth "
+ " FROM user_auth "
+ " WHERE username = ? "
;
userDetailsManager.setUsersByUsernameQuery(sql1);
userDetailsManager.setAuthoritiesByUsernameQuery(sql2);
return userDetailsManager;
}
}
Java
๋ณต์ฌ