λ°λ‘ λ‘κ·ΈμΈ
μ΄μ νμ΄μ§
μ΄μ νμ΄μ§ λ΄μ©μ μ΄μ΄μ μ§νν©λλ€.
Code
Preview
1.
νμ κ°μ
νλ©΄
2.
λ©μΈ νλ©΄
μμ νλ‘μΈμ€
1.
νλ‘μ νΈ μμ±
2.
νλ‘μ νΈ μ€μ
3.
μλΉμ€
4.
컨νΈλ‘€λ¬
Preview
1.
νμ κ°μ
νλ©΄
2.
λ©μΈ νλ©΄
νμ κ°μ νλ©΄
νμ κ°μ
μ²λ¦¬ λ°λ‘ λ‘κ·ΈμΈ λ©μΈ νλ©΄
νμ κ°μ
μμ² μ, νμ κ°μ
μ΄ μ±κ³΅νκ² λλ©΄ λ°λ‘ λ‘κ·ΈμΈ μ²λ¦¬ ν λ©μΈ νλ©΄μΌλ‘ μ΄λν©λλ€.
λ©μΈ νλ©΄
μμ νλ‘μΈμ€
1.
νλ‘μ νΈ μμ±
2.
νλ‘μ νΈ μ€μ
3.
μ€νλ§ μνλ¦¬ν° μ€μ
4.
μλΉμ€
5.
컨νΈλ‘€λ¬
νλ‘μ νΈ μμ±
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
볡μ¬
νλ‘μ νΈ μ€μ
application.properties
spring.application.name=form-jointologin
# λ°μ΄ν° μμ€ - MySQL
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/aloha?serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true&useSSL=false&autoReconnection=true&autoReconnection=true
spring.datasource.username=aloha
spring.datasource.password=123456
# Mybatis μ€μ
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.type-aliases-package=com.aloha.formjointologin.domain
mybatis.mapper-locations=classpath:mybatis/mapper/**/**.xml
Markdown
볡μ¬
μ€νλ§ μνλ¦¬ν° μ€μ
β’
~/config/CommonConfig.java
~/config/CommonConfig.java
AuthenticationManager λΉμ λ±λ‘ν©λλ€.
UserServiceImpl.java μμ μμ‘΄μ±μ μ£Όμ
λ°μ μ¬μ©νκΈ° μν΄μ μ€μ ν©λλ€.
@Configuration
public class CommonConfig {
/**
* π μνΈν λ°©μ λΉ λ±λ‘
* @return
*/
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
/**
* π AuthenticationManager λΉ λ±λ‘
* @param authenticationConfiguration
* @return
* @throws Exception
*/
@Bean
public AuthenticationManager authenticationManager(
AuthenticationConfiguration authenticationConfiguration)
throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
}
Java
볡μ¬
μλΉμ€
β’
UserService.java
β’
UserServiceImpl.java
UserService.java
login() λ©μλλ₯Ό μΆκ°λ‘ μ μν©λλ€.
public interface UserService {
// λ‘κ·ΈμΈ
public boolean login(Users user) throws Exception;
// μ‘°ν
public Users select(String username) throws Exception;
// νμ κ°μ
public int join(Users user) throws Exception;
// νμ μμ
public int update(Users user) throws Exception;
// νμ κΆν λ±λ‘
public int insertAuth(UserAuth userAuth) throws Exception;
}
Java
볡μ¬
UserServiceImpl.java
νμ κ°μ
μ, μ
λ ₯ν λ‘κ·ΈμΈ μμ΄λμ λΉλ°λ²νΈλ₯Ό ν΅ν΄ λ‘κ·ΈμΈ μΈμ¦ μ²λ¦¬κ° λλλ‘ κ΅¬νν©λλ€.
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private AuthenticationManager authenticationManager;
@Override
public boolean login(Users user) throws Exception {
// // π ν ν° μμ±
String username = user.getUsername(); // μμ΄λ
String password = user.getPassword(); // μνΈνλμ§ μμ λΉλ°λ²νΈ
UsernamePasswordAuthenticationToken token
= new UsernamePasswordAuthenticationToken(username, password);
// ν ν°μ μ΄μ©νμ¬ μΈμ¦
Authentication authentication = authenticationManager.authenticate(token);
// μΈμ¦ μ¬λΆ νμΈ
boolean result = authentication.isAuthenticated();
// μνλ¦¬ν° μ»¨ν
μ€νΈμ λ±λ‘
SecurityContextHolder.getContext().setAuthentication(authentication);
return result;
}
@Override
public Users select(String username) throws Exception {
Users user = userMapper.select(username);
return user;
}
@Override
public int join(Users user) throws Exception {
String username = user.getUsername();
String password = user.getPassword();
String encodedPassword = passwordEncoder.encode(password); // π λΉλ°λ²νΈ μνΈν
user.setPassword(encodedPassword);
// νμ λ±λ‘
int result = userMapper.join(user);
if( result > 0 ) {
// νμ κΈ°λ³Έ κΆν λ±λ‘
UserAuth userAuth = new UserAuth();
userAuth.setUsername(username);
userAuth.setAuth("ROLE_USER");
result = userMapper.insertAuth(userAuth);
}
return result;
}
@Override
public int update(Users user) throws Exception {
int result = userMapper.update(user);
return result;
}
@Override
public int insertAuth(UserAuth userAuth) throws Exception {
int result = userMapper.insertAuth(userAuth);
return result;
}
}
Java
볡μ¬
컨νΈλ‘€λ¬
- ~/controller/HomeController.java
~/controller/HomeController.java
νμ κ°μ
μ²λ¦¬ μ±κ³΅ μ, λ‘κ·ΈμΈ μμ² ν λ©μΈ νλ©΄μΌλ‘ μ΄λνλλ‘ μ½λλ₯Ό μμ ν©λλ€.
μ΄μ μ½λ
int result = userService.join(user);
if( result > 0 ) {
return "redirect:/login";
}
Java
볡μ¬
μμ μ½λ
// μνΈν μ λΉλ°λ²νΈ
String plainPassword = user.getPassword();
// νμ κ°μ
μμ²
int result = userService.join(user);
// νμ κ°μ
μ±κ³΅ μ, λ°λ‘ λ‘κ·ΈμΈ
if( result > 0 ) {
// μνΈν μ λΉλ°λ²νΈ λ€μ μΈν
// νμκ°μ
μ, λΉλ°λ²νΈ μνΈννκΈ° λλ¬Έμ,
user.setPassword(plainPassword);
userService.login(user);
return "redirect:/";
}
Java
볡μ¬
HomeController.java
@Slf4j
@Controller
public class HomeController {
@Autowired
private UserService userService;
/**
* λ©μΈ νλ©΄
* π [GET] - /
* π index.html
* @return
*/
@GetMapping("")
public String home() {
log.info(":::::::::: λ©μΈ νλ©΄ ::::::::::");
return "index";
}
/**
* νμ κ°μ
νλ©΄
* π [GET] - /join
* π join.html
* @return
*/
@GetMapping("/join")
public String join() {
log.info(":::::::::: νμ κ°μ
νλ©΄ ::::::::::");
return "join";
}
/**
* νμ κ°μ
μ²λ¦¬
* π [POST] - /join
* β‘ β ππλ°λ‘ λ‘κ·ΈμΈ β‘ /
* β /join?error
* @param user
* @return
* @throws Exception
*/
@PostMapping("/join")
public String joinPro(Users user) throws Exception {
log.info(":::::::::: νμ κ°μ
μ²λ¦¬ ::::::::::");
log.info("user : " + user);
// μνΈν μ λΉλ°λ²νΈ
String plainPassword = user.getPassword();
// νμ κ°μ
μμ²
int result = userService.join(user);
// νμ κ°μ
μ±κ³΅ μ, λ°λ‘ λ‘κ·ΈμΈ
if( result > 0 ) {
// μνΈν μ λΉλ°λ²νΈ λ€μ μΈν
// νμκ°μ
μ, λΉλ°λ²νΈ μνΈννκΈ° λλ¬Έμ,
user.setPassword(plainPassword);
userService.login(user);
return "redirect:/";
}
return "redirect/join?error";
}
/**
* μμ΄λ μ€λ³΅ κ²μ¬
* @param username
* @return
* @throws Exception
*/
@ResponseBody
@GetMapping("/check/{username}")
public ResponseEntity<Boolean> userCheck(@PathVariable("username") String username) throws Exception {
log.info("μμ΄λ μ€λ³΅ νμΈ : " + username);
Users user = userService.select(username);
// μμ΄λ μ€λ³΅
if( user != null ) {
log.info("μ€λ³΅λ μμ΄λ μ
λλ€ - " + username);
return new ResponseEntity<>(false, HttpStatus.OK);
}
// μ¬μ© κ°λ₯ν μμ΄λμ
λλ€.
log.info("μ¬μ© κ°λ₯ν μμ΄λ μ
λλ€." + username);
return new ResponseEntity<>(true, HttpStatus.OK);
}
}
Java
볡μ¬