1. ๐ค์คํ๋ง ์ํ๋ฆฌํฐ(Spring Security) ํ์์ ๋ณด ์์ ๊ตฌํํ๊ธฐ
1. ๋จผ์ ํ์ ์ ๋ณด ์์ ํ์ด์ง๋ก ์ด๋ํ๋ ์ฝ๋๋ฅผ UserController์ ์์ฑํ๋ค.
<java />
@GetMapping("/user/updateForm")
public String updateForm() {
return "user/updateForm";
}
2. ํ์ ์ ๋ณด ์์ ํ๋ฉด updateForm์ ์์ฑํ๋ค.
- username์ ์์ ํ ์ ์๋๋ก readonly๋ฅผ ๋ฃ์ด์ค๋ค.
- userid๋ hidden ์์ฑ์ผ๋ก ๋ฃ์ด์ค๋ค.
- user ์ ๋ณด๋ principal์์ ๊ฐ์ ธ์จ๋ค.

3. ํ์ ์ ๋ณด ์์ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ user.js์ ์์ฑํ๋ค.
- ์์ ์ด๋ฏ๋ก PUT ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌํ๋ค.
<javascript />
let index = {
init: function(){
$("#btn-update").on("click",()=>{ //function(){} ๋์ ()=>{} : this๋ฅผ ๋ฐ์ธ๋ฉํ๊ธฐ ์ํด์
this.update();
});
},
update: function(){
//alert('user์ saveํจ์ ํธ์ถ๋จ');
let data = {
id: $("#id").val(),
username: $("#usename").val(),
password: $("#password").val(),
email: $("#email").val(),
};
$.ajax({
//ํ์์ ๋ณด ์์ ์์ฒญ
type: "PUT",
url: "/user",
data: JSON.stringify(data), //http body ๋ฐ์ดํฐ
contentType: "application/json; charset=utf-8", //body ๋ฐ์ดํฐ๊ฐ ์ด๋ค ํ์
์ธ์ง(MIME)
dataType: "json" //์์ฒญ์ ์๋ฒ๋ก ํด์ ์๋ต์ด ์์ ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ชจ๋ ๊ฒ์ด ๋ฌธ์์ด(String)=>javascript ์ค๋ธ์ ํธ๋ก ๋ณ๊ฒฝ
//์๋ต ๊ฒฐ๊ณผ๊ฐ ์ ์์ผ ๋
}).done(function(resp){
alert("ํ์์์ ์ด ์๋ฃ๋์์ต๋๋ค.");
//console.log(resp);
location.href="/";
//์คํจ์ผ ๋
}).fail(function(error){
alert(JSON.stringify(error));
});
}
}
index.init();
4. UserService ์์ฑ
- @Transactional ์ ์ฉ
- ๋น๋ฐ๋ฒํธ ์ํธํ๋ฅผ ์ํด ์ธ์ฝ๋ฉ ์ ๋น๋ฐ๋ฒํธ์ ์ธ์ฝ๋ฉ ํ ์ด๋ฏธ์ง๋ฅผ ๋ฃ๊ณ ๊ทธ๊ฒ์ User ์ ๋ณด๊ฐ ๋ด๊ธด persistance์ ๋ด๋๋ค.

5. UserApiController ์์ฑ

๐ก์ฌ๊ธฐ๊น์ง ์์ฑํ๋ฉด DB์๋ ๋ณ๊ฒฝ๋ ๋น๋ฐ๋ฒํธ๊ฐ ๋ฐ์๋์ง๋ง ๋ธ๋ผ์ฐ์ ์ ๋ฐ์๋์ง ์๊ณ ๋ก๊ทธ์์ ํ ์ฌ๋ก๊ทธ์ธ์ ํด์ผ ๋ฐ์๋๋ค.


๐ก๊ทธ ์ด์ ๋ ์๋น์ค ์ฝ๋๊ฐ ์ ์ฉ๋๋ฉด ํธ๋์ญ์ ์ด ์ข ๋ฃ๋๊ธฐ ๋๋ฌธ์ DB ๊ฐ์ ๋ณ๊ฒฝ๋์ง๋ง ์ธ์ ๊ฐ์ ๋ณ๊ฒฝ๋์ง ์์ ์ํ์ด๋ฏ๋ก ์ฐ๋ฆฌ๊ฐ ๊ฐ์ ๋ก ์ธ์ ๊ฐ์ ๋ณ๊ฒฝํด์ฃผ์ด์ผ ํ๋ค.
๊ทธ๋ฐ๋ฐ ์คํ๋ง ์ํ๋ฆฌํฐ์์๋ ์ธ์ ๊ฐ์ ๋ณ๊ฒฝํ๋ ๋ฐ์ ๋ค์ ๋ณต์กํ ๊ณผ์ ์ ๊ฑฐ์น๋ค.

- SecurityContextHolder ์์ SecurityContext ์์ Authenitication ๊ฐ์ฒด๊ฐ ๋ค์ด๊ฐ -> ์ธ์ ๊ฐ์ด ์ ์ฅ๋ ์ํ!
- ๋ก๊ทธ์ธ ์์ฒญ์ด ์ค๋ฉด AuthenticationFilter๋ฅผ ๊ฑฐ์ณ์ UsernamePasswordAuthenticationToken์ ๋ง๋ฆ.
- AuthenticationManager๊ฐ ๊ทธ๋ฅ username,password๊ฐ ์๋ ํ ํฐ์ ๋ฐ์์ ๊ฐ์ฒด๋ฅผ ๋ง๋ฆ.
- ์ผ๋จ username๋ง ๋ค์ด์ค๊ณ ํด๋น user๊ฐ DB์ ์๋์ง ํ์ธํ๊ณ ์์ผ๋ฉด principal์ ๋์ ธ์ ์ธ์ ์ ๋ง๋ค์ด์ค.
- password๋ ์คํ๋ง์ด ๋ฐ๋ก ๊ฐ์ ธ๊ฐ์ ์ธ์ฝ๋ฉ์ ๊ฑฐ์ณ์ผํจ.
6. SecurityConfig ํด๋์ค์ @Bean ๋ฑ๋ก
<java />
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
// TODO Auto-generated method stub
return super.authenticationManagerBean();
}
7. UserApiController์ ์๋ ์ฝ๋ ์ถ๊ฐํ๋ฉด ๋!
<java />
Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword()));
SecurityContextHolder.getContext().setAuthentication(authentication);
return new ResponseDto<Integer>(HttpStatus.OK.value(),1);
'Springboot > Springboot Blog Project' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ์๊ฐ์ ๋ฌธ์ ์ ๊ฒ์๊ธ ์ญ์ ๋ฌธ์ ํด๊ฒฐ (0) | 2022.09.27 |
---|---|
์นด์นด์ค ๋ก๊ทธ์ธ API ์๋น์ค ๊ตฌํํ๊ธฐ (0) | 2022.09.25 |
์คํ๋ง ์๋ ์๋ฆฌ ๋ณต์ต! ์ค์โโ (0) | 2022.09.25 |
๊ธ ์์ธ๋ณด๊ธฐ, ๊ธ ์ญ์ ํ๊ธฐ, ๊ธ ์์ ํ๊ธฐ ๊ตฌํํ๊ธฐ (1) | 2022.09.25 |
๊ธ์ฐ๊ธฐ, ๊ธ ๋ชฉ๋ก๋ณด๊ธฐ, ํ์ด์ง ๊ตฌํํ๊ธฐ (0) | 2022.09.25 |