Last active
May 15, 2019 10:13
-
-
Save ohjuntaek/1f83fd393b294ca8ac92ce1f7b9cdf06 to your computer and use it in GitHub Desktop.
시큐리티 적용한것들
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ssafy.safefood.config; | |
import java.io.IOException; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import javax.servlet.http.HttpSession; | |
import org.springframework.security.core.Authentication; | |
import org.springframework.security.core.context.SecurityContextHolder; | |
import org.springframework.security.core.userdetails.UserDetails; | |
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; | |
public class CustomLoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler { | |
public CustomLoginSuccessHandler(String defaultTargetUrl) { | |
setDefaultTargetUrl(defaultTargetUrl); | |
} | |
@Override | |
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, | |
Authentication authentication) throws ServletException, IOException { | |
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | |
UserDetails userDetails = (UserDetails) principal; | |
HttpSession session = request.getSession(); | |
session.setAttribute("id", userDetails.getUsername()); | |
super.onAuthenticationSuccess(request, response, authentication); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ssafy.safefood.config; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.List; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.security.core.GrantedAuthority; | |
import org.springframework.security.core.authority.SimpleGrantedAuthority; | |
import org.springframework.security.core.userdetails.UserDetails; | |
import org.springframework.security.core.userdetails.UserDetailsService; | |
import org.springframework.security.core.userdetails.UsernameNotFoundException; | |
import org.springframework.security.crypto.password.PasswordEncoder; | |
import org.springframework.stereotype.Service; | |
import com.ssafy.safefood.model.repository.UserDAO; | |
import com.ssafy.safefood.model.vo.UserVO; | |
@Service | |
public class CustomUserDetailsService implements UserDetailsService { | |
@Autowired | |
UserDAO userDAO; | |
@Autowired | |
PasswordEncoder passwordEncoder; | |
@Override | |
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | |
UserVO userVO = userDAO.findById(username); | |
if (userVO != null) { | |
List<String> roles = new ArrayList<>(); | |
roles.add("ROLE_USER"); | |
userVO.setAuthorities(makeGrantedAuthority(roles)); | |
} | |
return new SecurityUser(userVO); | |
} | |
private Collection<? extends GrantedAuthority> makeGrantedAuthority(List<String> roles) { | |
List<GrantedAuthority> list = new ArrayList<>(); | |
roles.forEach(role -> list.add(new SimpleGrantedAuthority(role))); | |
return list; | |
} | |
public void save(UserVO userVO, String role) { | |
userVO.setPass(passwordEncoder.encode(userVO.getPass())); | |
userDAO.insertUser(userVO); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ssafy.safefood.config; | |
import org.springframework.security.core.userdetails.User; | |
import org.springframework.security.core.userdetails.UserDetails; | |
import com.ssafy.safefood.model.vo.UserVO; | |
public class SecurityUser extends User { | |
private static final long serialVersionUID = 1L; | |
private String ip; | |
public SecurityUser(UserVO member) { | |
super(member.getId(), member.getPass(), member.getAuthorities()); | |
} | |
public String getIp() { | |
return ip; | |
} | |
public void setIp(String ip) { | |
this.ip = ip; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ssafy.safefood.model.vo; | |
import java.util.Collection; | |
import org.apache.ibatis.type.Alias; | |
import org.springframework.security.core.GrantedAuthority; | |
@Alias("user") | |
public class UserVO { | |
private String id; | |
private String pass; | |
private String name; | |
private String address; | |
private String tel; | |
private String allergy; | |
private Collection<? extends GrantedAuthority> authorities; | |
public Collection<? extends GrantedAuthority> getAuthorities() { | |
return authorities; | |
} | |
public void setAuthorities(Collection<? extends GrantedAuthority> authorities) { | |
this.authorities = authorities; | |
} | |
public UserVO(String id, String pass, String name, String address, String tel, String allergy) { | |
this.id = id; | |
this.pass = pass; | |
this.name = name; | |
this.address = address; | |
this.tel = tel; | |
this.allergy = allergy; | |
} | |
public UserVO() { | |
} | |
@Override | |
public String toString() { | |
return "UserVO [id=" + id + ", pass=" + pass + ", name=" + name + ", address=" + address + ", tel=" + tel | |
+ ", allergy=" + allergy + "]"; | |
} | |
public String getId() { | |
return id; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public String getPass() { | |
return pass; | |
} | |
public void setPass(String pass) { | |
this.pass = pass; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getAddress() { | |
return address; | |
} | |
public void setAddress(String address) { | |
this.address = address; | |
} | |
public String getTel() { | |
return tel; | |
} | |
public void setTel(String tel) { | |
this.tel = tel; | |
} | |
public String getAllergy() { | |
return allergy; | |
} | |
public void setAllergy(String allergy) { | |
this.allergy = allergy; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ssafy.safefood.config; | |
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.web.builders.HttpSecurity; | |
import org.springframework.security.config.annotation.web.builders.WebSecurity; | |
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.crypto.password.PasswordEncoder; | |
import org.springframework.security.web.authentication.AuthenticationSuccessHandler; | |
//https://4urdev.tistory.com/53 | |
@Configuration | |
@EnableWebSecurity | |
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | |
// @Autowired | |
// AuthenticationProvider authenticationProvider; | |
// 이건 뭐가 다른거야? | |
@Autowired | |
CustomUserDetailsService customUserDetailsService; | |
@Bean | |
public PasswordEncoder passwordEncoder() { | |
return new BCryptPasswordEncoder(); | |
} | |
@Bean | |
public AuthenticationSuccessHandler successHandler() { | |
return new CustomLoginSuccessHandler("/"); | |
} | |
@Override | |
public void configure(WebSecurity web) throws Exception { | |
web.ignoring().antMatchers("/"); | |
} | |
@Override | |
protected void configure(HttpSecurity http) throws Exception { | |
http | |
.cors() | |
.and() | |
.csrf() | |
.disable() | |
.authorizeRequests() | |
.antMatchers("/","/resources/**", "/css/**", "/js/**", "/img/**", "/scss/**", "/vendor/**", "/signup").permitAll() | |
.anyRequest().authenticated() | |
.and() | |
.formLogin() | |
.loginPage("/login") | |
.permitAll() | |
.usernameParameter("id") | |
.passwordParameter("pass") | |
.successHandler(successHandler()) | |
.and() | |
.logout() | |
.logoutUrl("/logout") | |
.permitAll(); | |
} | |
@Override | |
protected void configure(AuthenticationManagerBuilder auth) throws Exception { | |
auth.userDetailsService(customUserDetailsService) | |
.passwordEncoder(passwordEncoder()); // 여기서 확인해서 하는갑다... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment