@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new SessionIntercepter())
.addPathPatterns("/user/profile/**")
.addPathPatterns("/post/write/**")
.addPathPatterns("/post/update/**")
.addPathPatterns("/post/delete/**");
// addExcludePatterns() ์ ์ธ ์ํฌ ๋ ์ฌ์ฉ!!
}
1. ์ํ๋ฆฌํฐ๋ฅผ ์ฌ์ฉํ๊ธฐ์ํ ์ญ์ ํ์ผ
- WebConfig.java
์ธํฐ์ ํฐ ์ฌ์ฉ ์ํจ
- SessionIntercepter
ํต์งธ๋ก ๋ ๋ฆผ, ์ธํฐ์ ํฐ ์๋ ์ํจ
- pom.xml์ security์ฃผ์ ํ๊ธฐ
๋น๋ฐ๋ฒํธ
๊ธฐ๋ณธ์ ์ผ๋ก user, ๋น๋ฐ๋ฒํธ๋ ์ฝ์์ฐฝ์ ์๋๊ฒ ๋ณต๋ถ ํ๋ฉด ๋์ด!
๊ธฐ๋ณธ์ ์ผ๋ก ์ธ์ ์ ์ด๋ฉด ์ ๊ณต๋๋ ์คํ๋ง ์ํ๋ฆฌํฐ ์ปจํ ์คํธ ์ธ์
์ด๊ณณ์ ์ด์ ํฐ์ผ์ด์ ์ด ๋ค์ด๊ฐ๋ค.
2. SecurityConfig.java ์์ฑ
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) // ์ง์
๋๊ธฐ ์ง์ ์ ๋ฉ๋ชจ๋ฆฌ์ ๋์ธ ๊ฒ ์ด๋ค.
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception { //๋ชจ๋ ์์ฒญ์ ๋ฐ๋๋ค.
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/user/profile/**","/post/write/**","/post/update/**","/post/delete/**","/post/detail/**").authenticated()
.anyRequest().permitAll()
.and() //์๋๋ก์ด๋ ์ฑ ๊ฐ์ ๊ฒฝ์ฐ๋ ํผ ๋ก๊ทธ์ธ์ด ์๋๋ค. ์น์์๋ ํผ ๋ก๊ทธ์ธ๋ฐฉ์์ ๋ง์ด ์ด๋ค.
.formLogin()
.loginPage("/user/login") //๊ทธ๋ฅ ์ฃผ์๋ก ๊ฐ๊ณ ์ถ์ผ๋ฉด loginProcessingUrl()๋ก ๊ฐ๋ฉด ๋๊ณ , ๊ทธ ์ ์ ๋ฌด์ธ๊ฐ ์ํํ๊ณ ์ถ์ผ๋ฉดsuccessHandler์ ์ฌ์ฉํ์ฌ new ํ ์ ์๋ค.
.loginProcessingUrl("/user/login") // POST๋ง ๋์ ์ฑ
.successHandler(new AuthenticationSuccessHandler() {
@Override // authentication์ ๋์ ์ธ์ฆ ๊ฐ์ฒด๊ฐ ๋ค์ด๊ฐ ์๋ค.
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
// ๊ทธ ์ ์ user์ ์ ๋ณด๋ฅผ ๋ณด๊ณ ๋ธ๋๋ฆฌ์คํธ, ํํฐ๋ ํ ์ ์๋ค.
response.sendRedirect("/");
}
});
}
}
package com.cos.blog.config;
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;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) // ์ง์
๋๊ธฐ ์ง์ ์ ๋ฉ๋ชจ๋ฆฌ์ ๋์ธ ๊ฒ ์ด๋ค.
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception { //๋ชจ๋ ์์ฒญ์ ๋ฐ๋๋ค.
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/user/profile/**","/post/write/**","/post/update/**","/post/delete/**","/post/detail/**").authenticated()
.anyRequest().permitAll()
.and() //์๋๋ก์ด๋ ์ฑ ๊ฐ์ ๊ฒฝ์ฐ๋ ํผ ๋ก๊ทธ์ธ์ด ์๋๋ค. ์น์์๋ ํผ ๋ก๊ทธ์ธ๋ฐฉ์์ ๋ง์ด ์ด๋ค.
.formLogin()
.loginPage("/user/login") // ๊ทธ๋ฅ ์ฃผ์๋ก ๊ฐ๊ณ ์ถ์ผ๋ฉด defaultSuccessUrl()๋ก ๊ฐ๋ฉด ๋๊ณ , ๊ทธ ์ ์ ๋ฌด์ธ๊ฐ ์ํํ๊ณ ์ถ์ผ๋ฉดsuccessHandler์ ์ฌ์ฉํ์ฌ new ํ ์ ์๋ค.
.loginProcessingUrl("/user/login") // POST๋ง ๋์ ์ฑ
.defaultSuccessUrl("/");// successHamdler์ฌ์ฉ ๊ฐ๋ฅ
}
}
์ด๋๊น์ง ํ๋ฉด userdetail์ด ์๊ธฐ๋๋ฌธ์ ๋์ ์ฑ์ง ๋ชปํ๋ค.
detail์ ์์๋ฐ์ ๊ตฌํํ๋ ์์ ์ ํ์ฌ์ผ ํ๋ค.
3. MyUserDetailService์์ฑ(detailservice๋ฅผ ์์๋ฐ์)
์ ์ ๋ค์ ํจ์ค์๋ํํฐ๊ฐ ์๋ํ๋ฉด์ ๋ง๋ ํ ํฐ์ ์ ๋ฌ ๋ฐ๋๋ค.
- MyUserDetailService.java์์ฑ
package com.cos.blog.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import com.cos.blog.model.user.User;
import com.cos.blog.repository.UserRepository;
@Service
public class MyUserDetailService implements UserDetailsService{
@Autowired
private UserRepository userRepository;
@Override // ํจ์ค์๋๋ ๊ฐ์งํ๊ณ ์๊ณ ์ ์ ๋ค์๋ง ์ ๋ฌ ํด์ค๋ค.
//์ ์ ํ๋ก๋ฐ์ด๋๋ ์ ์ ๋ํ
์ผ์ ๋ง๋ ๋ค.
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
System.out.println("username : "+username);
User user = userRepository.authentication(username);
if (user==null) {
throw new UsernameNotFoundException("ํด๋น ์ ์ ๊ฐ ์์ต๋๋ค. ");
}
return user;
}
}
๋ด๊ฐ ์ฌ์ฉํ๋ ์ ์ ๋ชจ๋ธ์ ์ ์ ๋ํ ์ผ์ค๋ฅผ ์ํ๋ฆฌ๋จผํธํด์ผํ๋ค.
-User.java ์์ ( implements UserDetails )
package com.cos.blog.model.user;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
// MaBatis์์ ResultType์ผ๋ก ๋ด์ ๋ ์์ฑ์ ํน์ Setter์ค ๋ฌด์์ด ํธ์ถ๋๋์ง ํ์ธ ํ Lombok ๋ณ๊ฒฝ
@Data
@NoArgsConstructor
public class User implements UserDetails{
private int id;
private String username;
private String password;
private String email;
private String profile;
private String role; // USER,MANAGER,ADMIN
private Timestamp createDate;
@Builder
public User(String username, String password, String email, String profile, String role) {
super();
this.username = username;
this.password = password;
this.email = email;
this.profile = profile;
this.role = role;
}
// username๊ณผ password์ getter๋ ๋ง๋ค์ด์ ธ์ผ ํ๋๋ฐ
// ์ฐ๋ฆฌ๋ ํ๋๋ช
์ username๊ณผ password๋ก ๋ง๋ค์๊ณ lombok ์ด๋
ธํ
์ด์
์ ๋ถ์ฌ๋์
// ์๋ง๋ค์ด๋๋๋ค.
// ๊ถํ์ด ๋ช๊ฐ ๋์ด์๋๊ฐ? ์ฌ๋ฌ๊ฐ์ ๊ถํ์ ๋ฆฌํดํ๋ค.
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Collection<SimpleGrantedAuthority> collectors = new ArrayList<>(); // ๋ด๊ฐ GrantedAuthority ์์๋ฐ๋ ๊ฐ์ฒด๋ฅผ new ํด๋๋๋ค.
collectors.add(new SimpleGrantedAuthority("ROLE_"+role));
// "ROLE_"+role ๋ก ํด์ผ์ง ์คํ๋ง์ด ์ธ์ํ๋ค. ๊ท์น!
return collectors;
}
// ๊ณ์ ์ด ๋ง๋ฃ๋์๋๊ฐ๋ฅผ ์ฒดํฌํ์ฌ ๋ฆฌํดํ๋ค(true:๋ง๋ฃ์๋จ)
// ์ข ๋ ๊น๊ฒ ๋ค์ด๊ฐ๋ฉด ๋ง์ง๋ง ์ ์์๊ฐ๊ณผ ํ์ฌ ์ธ์
์ ๋น๊ตํ๋ ๋ฑ์ผ๋ก ํ์ฉ ํ ์ ์๋ค.
@Override
public boolean isAccountNonExpired() {
return true;
}
// ๊ณ์ ์ด ์ ๊ฒจ์๋์ง ์ฒดํฌํ์ฌ ๋ฆฌํดํ๋ค(true:์ ๊ธฐ์ง ์์)
@Override
public boolean isAccountNonLocked() {
return true;
}
// ๋น๋ฐ๋ฒํธ๊ฐ ๋ง๋ฃ๋์๋์ง ์ฒดํฌํ์ฌ ๋ฆฌํดํ๋ค.(true:๋ง๋ฃ์๋จ)
@Override
public boolean isCredentialsNonExpired() {
return true;
}
// ํด๋น๊ณ์ ์ด ํ์ฑํ ๋์ด์๋์ง ์ฒดํฌํ์ฌ ๋ฆฌํดํ๋ค(true:ํ์ฑํ)
@Override
public boolean isEnabled() {
return true;
}
}
username, password์ getter, setter์ด ๋ง๋ค์ด์ ธ์๊ธฐ๋๋ฌธ์ ๋ฐ๋ก ์ค๋ฒ๋ผ์ด๋ ์ํด๋๋๋ค.
hasRole์ด ๋ชจ์ด๋ฉด getAuthorities๋ก ํธ์ถ ํด์ผํ๋ค.
์ด ๋ชจ๋ ๊ฒ์ ํ๋ฉด ์ธ์ ์ ์คํ๋ง์ํ๋ฆฌํฐ๋ฅผ ๋ฃ์ด๋๋๋ค.์คํ๋ง์ํ๋ฆฌํฐ ์์๋ ์ด์ ํฐ๊ฐ ์๋ค.
4. ์์ด๋์ ํจ์ค์๋ ์ ์ฅ ์ ํจ์ค์๋ hash๋ก ์ํธํํ๊ธฐ
-securityConfig ์์
์์ฆ์ SHA256๋ฅผ ์ฌ์ฉํ๋ค. ์คํ๋ง์ BCrypt ์ํธํ ๋ฐฉ์์ ์ฌ์ฉํ๋ค.
๋ณตํธํ๊ฐ ์๋จ. ์ฆ ๋น๋ฐ๋ฒํธ๋ฅผ ์์ด๋ฒ๋ฆฌ๋ฉด ๋๋๋ชจ๋ฅด๊ณ ๋๋๋ชจ๋ฅด๊ณ ๋ชจ๋๊ฐ ๋ชจ๋ฆ.
@Bean
public BCryptPasswordEncoder Encoded() {
return new BCryptPasswordEncoder();
}
๋น์ ๋ถ์ฌ์ผ ๋ฉ๋ชจ๋ฆฌ์ ๋ฌ๋ค.
-UserService.java
package com.cos.blog.service;
import javax.servlet.http.HttpSession;
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;
import com.cos.blog.model.ReturnCode;
import com.cos.blog.model.user.User;
import com.cos.blog.model.user.dto.ReqJoinDto;
import com.cos.blog.model.user.dto.ReqLoginDto;
import com.cos.blog.repository.UserRepository;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private HttpSession session;
@Autowired
private BCryptPasswordEncoder passwordEncoder;
// result = 0 ๋น์ ์, 1 ์ ์, -1 DB ์ค๋ฅ, -2 ์์ด๋ ์ค๋ณต
@Transactional
public int ํ์๊ฐ์
(ReqJoinDto dto) {
try {
int result = userRepository.findByUsername(dto.getUsername());
if(result == 1) {
return ReturnCode.์์ด๋์ค๋ณต;
}else {
//ํจ์ค์๋ ์ํธํํ๊ธฐ
String encodePassword = passwordEncoder.encode(dto.getPassword());
dto.setPassword(encodePassword);
return userRepository.save(dto);
}
} catch (Exception e) {
throw new RuntimeException();
}
}
public User ๋ก๊ทธ์ธ(ReqLoginDto dto) {
return userRepository.findByUsernameAndPassword(dto);
}
public int ์์ ์๋ฃ(int id, String password, String profile) {
int result = userRepository.update(id, password, profile);
if(result == 1) { // ์์ ์ฑ๊ณต
User user = userRepository.findById(id);
session.setAttribute("principal", user);
return 1;
}else { // ์์ ์คํจ
return -1;
}
}
}
- SecurityConfig
๋ก๊ทธ์ธํ ๋์๋ ์ํธํํ ์ ์๊ฒ
@Autowired
protected UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(encoded());
}
- usercontroller ์ญ์
@PostMapping("/user/login")
public ResponseEntity<?> login(
@Valid @RequestBody ReqLoginDto dto,
BindingResult bindingResult
) {
// request ๊ฒ์ฆ = AOP๋ก ์ฒ๋ฆฌํ ์์
// ์๋น์ค ํธ์ถ
User principal = userService.๋ก๊ทธ์ธ(dto);
if(principal != null) {
session.setAttribute("principal", principal);
return new ResponseEntity<RespCM>(new RespCM(200, "ok"), HttpStatus.OK);
}else {
return new ResponseEntity<RespCM>(new RespCM(400, "fail"), HttpStatus.BAD_REQUEST);
}
}
๊ธฐ์กด์ ์์ด์์ค๋ก ์์ฒญํ๊ณ ๋ฐ์๋ ๊ฒ์ / ๋ก ๋ฆฌํดํด์ฃผ๋ ๊ฒ์ด ์๋ ์ง์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ jsonํํ๋ก ๋ณ๊ฒฝํ๊ฑฐ๋ ํด์ผํ๋ค. ๋งคํผ๊ฐ ๋ค๊ณ ์๋ ์ค๋ธ์ ํธ ๋ณํํ๋๊ฒ์ ์ฌ์ฉ
- SecurityConfig.java ์์
package com.cos.blog.config;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import com.cos.blog.model.RespCM;
import com.fasterxml.jackson.databind.ObjectMapper;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) // ์ง์
๋๊ธฐ ์ง์ ์ ๋ฉ๋ชจ๋ฆฌ์ ๋์ธ ๊ฒ ์ด๋ค.
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Bean
public BCryptPasswordEncoder encoded() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception { //๋ชจ๋ ์์ฒญ์ ๋ฐ๋๋ค.
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/user/profile/**","/post/write/**","/post/update/**","/post/delete/**","/post/detail/**").authenticated()
.antMatchers("/amdin/**").access("hasRole('ROLE_ADMIN')or hasRole('ROLE_MANAGER')")//๊ถํ ์ค์ ,USER.java์์๋ ์ค์ ํด์ฃผ๊ณ access๋ง ํด์ค๋ค.
.anyRequest().permitAll()
.and() //์๋๋ก์ด๋ ์ฑ ๊ฐ์ ๊ฒฝ์ฐ๋ ํผ ๋ก๊ทธ์ธ์ด ์๋๋ค. ์น์์๋ ํผ ๋ก๊ทธ์ธ๋ฐฉ์์ ๋ง์ด ์ด๋ค.
.formLogin()
.loginPage("/user/login") // ๊ทธ๋ฅ ์ฃผ์๋ก ๊ฐ๊ณ ์ถ์ผ๋ฉด defaultSuccessUrl()๋ก ๊ฐ๋ฉด ๋๊ณ , ๊ทธ ์ ์ ๋ฌด์ธ๊ฐ ์ํํ๊ณ ์ถ์ผ๋ฉดsuccessHandler์ ์ฌ์ฉํ์ฌ new ํ ์ ์๋ค.
.loginProcessingUrl("/user/loginProc") // POST๋ง ๋์ ์ฑ
.failureHandler(new AuthenticationFailureHandler() {
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) throws IOException, ServletException {
System.out.println("์คํจ~~~~~~~~~~");
System.out.println(exception.getMessage());
System.out.println(exception.getLocalizedMessage());
System.out.println(exception.toString());
}
})
.successHandler(new AuthenticationSuccessHandler() {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
PrintWriter out= response.getWriter();
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(new RespCM(200,"ok"));
System.out.println(jsonString);
out.print(jsonString);
out.flush();
}
});
//.defaultSuccessUrl("/");// successHamdler์ฌ์ฉ ๊ฐ๋ฅ , ํ์ด์ง๋ฅผ ๋ฆฌํด ํด์ค๋ค.
}
@Autowired
protected UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(encoded());
}
}
-login.jsp
๋ก๊ทธ์ธ ํผ๋ ํผ์ ์ ์กํ ์์๊ฒ ์์ !
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="../include/nav.jsp"%>
<div class="container">
<form>
<div class="form-group">
<label for="username">์ ์ ๋ค์</label>
<input type="text" class="form-control" placeholder="Enter Username" id="username">
</div>
<div class="form-group">
<label for="password">ํจ์ค์๋</label>
<input type="password" class="form-control" placeholder="Enter password" id="password">
</div>
</form>
<button id="login--submit" class="btn btn-primary">๋ก๊ทธ์ธ</button>
</div>
<script>
$('#login--submit').on('click', function(e){
// e.preventDefault();
var data = {
username: $('#username').val(),
password: $('#password').val()
};
alert(data.username);
alert(data.password);
$.ajax({
type: 'POST',
url: '/user/loginProc',
data: data,
contentType : 'application/x-www-form-urlencoded',
dataType : 'json'
}).done(function(r){
console.log(r);
alert("๋ก๊ทธ์ธ ์ฑ๊ณต");
location.href = '/';
}).fail(function(r){
console.log(r);
alert("๋ก๊ทธ์ธ ์คํจ");
});
});
</script>
<%@include file="../include/footer.jsp"%>
5. ๊ธฐ์กด์ ์ธ์ ์ฝ๋ ์์
pom.xml์ ์ถ๊ฐ
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
</dependency>
์๋์ ์ํ๋ฆฌํฐ ํ๊ทธ๋ฅผ ์์ ๋ถ์ด๋ฉด ์ํ๋ฆฌํฐ ์ ์ฉ ํ๊ทธ๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<sec:authorize access="isAuthenticated()">
<sec:authentication property="principal" var="principal" />
</sec:authorize>
์คํ๋ง์ํ๋ฆฌํฐ์ ์๋ ์ ๋ณด๊ฐ ์๋์ง๋ฅผ ํ์ธํด์ค true๊ฐ ๋จ์ด์ง๋ฉด ์๋ ๊ฒ์ด๋ค.
๊ทธ ์ธ์ ๊ฐ์ principal์ ๋ฃ๋๋ค๋ ๋ป
์๋๋ principal์ ์ด๋ฆ์ ์ง์ ํด์ฃผ์์ง๋ง ์ด ํ๊ทธ๋ฅผ ์ฐ๋ฉด ์๋์ผ๋ก principal์ด๋ผ๊ณ ์ง์ ์ด ๋๋ค.
- nav.jsp ์์
sessionScope.principal์ ์ ๋ถ ๋ณ๊ฒฝํด ์ค๋ค.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<sec:authorize access="isAuthenticated()">
<sec:authentication property="principal" var="principal" />
</sec:authorize>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๋ธ๋ก๊ทธ</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<link rel="stylesheet" href="/css/style.css" />
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<nav class="navbar navbar-expand-md bg-info navbar-dark">
<!-- Brand -->
<a class="navbar-brand" href="/">Cos</a>
<!-- Toggler/collapsibe Button -->
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<!-- Navbar links -->
<div class="collapse navbar-collapse" id="collapsibleNavbar">
<ul class="navbar-nav">
<c:choose>
<c:when test="${not empty principal}">
<li class="nav-item">
<a class="nav-link" href="/post/write">๊ธ์ฐ๊ธฐ</a></li>
<li class="nav-item">
<a class="nav-link" href="/user/profile/${principal.id}">ํ์์ ๋ณด์์ </a></li>
<li class="nav-item">
<a class="nav-link" href="/user/logout">๋ก๊ทธ์์</a></li>
</c:when>
<c:otherwise>
<li class="nav-item">
<a class="nav-link" href="/user/join">ํ์๊ฐ์
</a></li>
<li class="nav-item">
<a class="nav-link" href="/user/login">๋ก๊ทธ์ธ</a></li>
</c:otherwise>
</c:choose>
</ul>
<img src="/media/${principal.profile}" class="rounded-circle my__img ml-auto"
width="30px" height="30px" onerror="javascript:this.src = '/images/unknown.jpg' " />
</div>
</nav>
<br />
- postController.java ์์
์ธ์ ์ ์ง์ฐ๊ณ ์๋์ฒ๋ผ ๋งค๊ฐ๋ณ์์ ์ด๋ ธํ ์ด์ ์ ๊ฑธ์ด ์ฌ์ฉํ์ฌ๋๋๋ค.
// ์ธ์ฆ ์ฒดํฌ
@PostMapping("/post/write")
public ResponseEntity<?> write(@RequestBody ReqWriteDto dto,@AuthenticationPrincipal User principal) {
dto.setUserId(principal.getId());
int result = postService.๊ธ์ฐ๊ธฐ(dto);
if(result == 1) {
return new ResponseEntity<RespCM>(new RespCM(200, "ok"), HttpStatus.OK);
}else {
return new ResponseEntity<RespCM>(new RespCM(400, "fail"), HttpStatus.BAD_REQUEST);
}
}
- userController์์
package com.cos.blog.controller;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.cos.blog.model.RespCM;
import com.cos.blog.model.ReturnCode;
import com.cos.blog.model.user.User;
import com.cos.blog.model.user.dto.ReqJoinDto;
import com.cos.blog.service.UserService;
import com.cos.blog.util.Script;
@Controller
public class UserController {
@Value("${file.path}")
private String fileRealPath; // ์๋ฒ์ ๋ฐฐํฌํ๋ฉด ๊ฒฝ๋ก ๋ณ๊ฒฝํด์ผํจ.
@Autowired
private UserService userService;
@GetMapping("/user/join")
public String join() {
return "/user/join";
}
@GetMapping("/user/login")
public String login() {
return "/user/login";
}
// ์ธ์ฆ, ๋์ผ์ธ ์ฒดํฌ
@GetMapping("/user/profile/{id}")
public String profile(@PathVariable int id,@AuthenticationPrincipal User principal) {
System.out.println("UserController : profile : "+principal.getProfile());
if(principal.getId() == id) {
return "/user/profile";
}else {
// ์๋ชป๋ ์ ๊ทผ์
๋๋ค. ๊ถํ์ด ์์ต๋๋ค.
return "/user/login";
}
}
// form:form ์ฌ์ฉํจ!!
@PutMapping("/user/profile")
public @ResponseBody String profile(
@RequestParam int id,
@RequestParam String password,
@RequestParam MultipartFile profile){
UUID uuid = UUID.randomUUID();
String uuidFilename = uuid+"_"+profile.getOriginalFilename();
// nio ๊ฐ์ฒด!!
Path filePath = Paths.get(fileRealPath+uuidFilename);
try {
Files.write(filePath, profile.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
int result = userService.์์ ์๋ฃ(id, password, uuidFilename);
if(result == 1) {
return Script.href("์์ ์๋ฃ", "/");
}else {
return Script.back("์์ ์คํจ");
}
}
// ๋ฉ์์ง ์ปจ๋ฒํฐ(Jackson Mapper)๋ request๋ฐ์ ๋ setter๋ก ํธ์ถํ๋ค.
@PostMapping("/user/join")
public ResponseEntity<?> join(@Valid @RequestBody ReqJoinDto dto, BindingResult bindingResult) {
int result = userService.ํ์๊ฐ์
(dto);
if(result == ReturnCode.์์ด๋์ค๋ณต) {
return new ResponseEntity<RespCM>(new RespCM(ReturnCode.์์ด๋์ค๋ณต, "์์ด๋์ค๋ณต"), HttpStatus.OK);
}else if(result == ReturnCode.์ฑ๊ณต) {
return new ResponseEntity<RespCM>(new RespCM(200, "ok"), HttpStatus.OK);
}else {
return new ResponseEntity<RespCM>(new RespCM(500, "fail"), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
๋ฑ session์ ์ฐ๋ ๋ชจ๋ ๊ณณ์ ์ฌ์ฉํ๋ฉด ๋๋ค.
๋ง์ง๋ง ์์ :-)
์๊ณ ํ์ ง์ต๋๋น ํฟ
'java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์คํ๋ง ๋ ๊ฑฐ์ - ๊ฒ์ํ๋ง๋ค๊ธฐ(1) ํ๊ฒฝ์ค์ (0) | 2021.02.27 |
---|---|
์คํ๋ง AOP (0) | 2021.02.27 |
์คํ๋ง - security (0) | 2021.02.27 |
์คํ๋ง ๊ฒ์ํ๋ง๋ค๊ธฐ-ํ์์์ ํ๋กํ ์ ๋ก๋ํ๊ธฐ (0) | 2021.02.27 |
์คํ๋ง๋ถํธ ๊ธฐ๋ณธ๊ฒ์ํ (0) | 2021.02.27 |