Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- 스프링 게시판 만들기
- 괄호 회전하기 파이썬
- 유니티기초
- 스프링부트 회원가입
- 스프링부트 미니프로젝트
- springboot 게시판
- 유니티
- spring jpa 게시판
- 파이썬 괄호 회전하기
- 스프링부트 블로그만들기
- 스프링부트 게시판만들기
- springboot 사이드프로젝트
- springboot 게시판 프로젝트
- typescript 기초문법
- 타입스크립트 기본문법
- 스프링부트 update
- JS기초
- 파이썬 기초
- python 괄호 회전하기
- 스프링게시판프로젝트
- 유니티Material
- 타입스크립트 기초
- 유니티Cube
- spring jpa 사이드프로젝트
- springboot 게시판만들기
- springboot 미니프로젝트
- 스프링부트 블로그
- jpa 게시판
- 타입스크립트 기초문법
- 프로그래머스 괄호 회전하기 python
Archives
- Today
- Total
Digking's cave
My First Blog Project (13) : 비밀번호 해쉬화 회원가입 / 권한에 따른 페이지 처리 본문
Spring/My First Blog Project
My First Blog Project (13) : 비밀번호 해쉬화 회원가입 / 권한에 따른 페이지 처리
디깅 2022. 12. 20. 14:00728x90
sercive에서 비밀번호 해쉬화 + role 세팅까지 하고
controller에서는 실제 save만 진행하도록 한다.
UserApiController.java
package com.cos.blog.controller.api;
import com.cos.blog.dto.ResponseDto;
import com.cos.blog.model.RoleType;
import com.cos.blog.model.User;
import com.cos.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
@RestController
public class UserApiController {
@Autowired
private UserService userService;
@PostMapping("/auth/joinProc")
public ResponseDto<Integer> save(@RequestBody User user) { // username, password, email
userService.회원가입(user);
return new ResponseDto<Integer>(HttpStatus.OK.value(), 1);
}
}
UserService.java
package com.cos.blog.service;
import com.cos.blog.model.RoleType;
import com.cos.blog.model.User;
import com.cos.blog.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private BCryptPasswordEncoder encoder;
@Transactional
public void 회원가입(User user){
String rawPassword = user.getPassword(); //1234 원문 비밀번호
String encPassword = encoder.encode(rawPassword); //해쉬 비밀번호
user.setPassword(encPassword);
user.setRoles(RoleType.USER);
userRepository.save(user);
}
}
BCryptPasswordEncoder
- SpringSecurity 에서 제공하는 클래스
- 비밀번호를 암호화 하는 메서드를 가졌다
.encode() 로 해쉬비밀번호화 한다.
SecurityConfig.java
package com.cos.blog.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
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.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
@Configuration //빈등록 (IoC관리)
@EnableWebSecurity //security 필터 등록
@EnableGlobalMethodSecurity(prePostEnabled = true) //특정 주소를 접근을 하면 권한 및 인증을 미리 체크하겠다는 뜻
public class SecurityConfig {
@Bean
public BCryptPasswordEncoder encoderPWD(){
return new BCryptPasswordEncoder();
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf().disable() //csrf 토큰 비활성화(테스트 시에는 걸어두는게 좋다)
.authorizeRequests()
.antMatchers("/","/auth/**","/js/**","/css/**","/image/**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/auth/loginForm");
return http.build();
}
}
권한없어도 메인과 js등 로그인에 필요한 화면들 접근 가능하도록 .antMatchers 주소 확장
- 해놓지 않으면, 로그인 버튼 누른 후에 js 등의 화면에 접근을 못함
반응형
'Spring > My First Blog Project' 카테고리의 다른 글
My First Blog Project (15) : 게시판 글쓰기 (0) | 2022.12.21 |
---|---|
My First Blog Project (14) : SpringSecurity 로그인 구현 (0) | 2022.12.21 |
My First Blog Project (12) : SpringSecurit 적용 및 로그인 페이지 생성 (0) | 2022.12.19 |
My First Blog Project (11) : 회원가입 / 로그인 처리 완료 (0) | 2022.12.18 |
My First Blog Project (10) : 블로그 메인 / 로그인 / 회원가입 화면 (0) | 2022.12.17 |