Search

λ°”λ‘œ 둜그인

λ°”λ‘œ 둜그인

Spring Security 5.7

이전 νŽ˜μ΄μ§€

이전 νŽ˜μ΄μ§€ λ‚΄μš©μ— μ΄μ–΄μ„œ μ§„ν–‰ν•©λ‹ˆλ‹€.

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
볡사