Skip to content

Commit

Permalink
security-oauth 회원가입로그인 인증 인가
Browse files Browse the repository at this point in the history
  • Loading branch information
dimes12 committed Jul 17, 2019
1 parent 961fe4f commit 0ec2d69
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.example.demo.config;

import com.example.demo.user.SecurityUserDetailsService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
Expand All @@ -26,6 +29,7 @@
public class AuthorizationServiceConfigurerAdapterImpl extends AuthorizationServerConfigurerAdapter {
private DataSource dataSource;
private AuthenticationManager authenticationManager;
private SecurityUserDetailsService securityUserDetailsService;

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
Expand All @@ -37,7 +41,9 @@ public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws E
endpoints
.approvalStore(approvalStore())
.tokenStore(tokenStore())
.authenticationManager(authenticationManager);
.authenticationManager(authenticationManager)
.userDetailsService(securityUserDetailsService)
;
}
@Bean
public TokenStore tokenStore() {
Expand Down
38 changes: 36 additions & 2 deletions src/main/java/com/example/demo/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.example.demo.user.SecurityUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
Expand All @@ -13,6 +15,12 @@
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;

@EnableWebSecurity
@Configuration
Expand All @@ -26,6 +34,11 @@ public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
public PasswordEncoder encoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}

@Bean
public AuthenticationSuccessHandler successHandler() {
return new SecurityLoginSuccessHandler("/gettoken");
Expand All @@ -40,17 +53,38 @@ protected AuthenticationManager authenticationManager() throws Exception {

@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
http.cors().and().csrf().disable()
.authorizeRequests()
.antMatchers("/", "/login", "/signup").permitAll()
.antMatchers("/", "/login", "/signup", "/posts").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.successHandler(successHandler());
}

@Bean
public FilterRegistrationBean customCorsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("http://localhost:8080");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));

//IMPORTANT #2: I didn't stress enough the importance of this line in my original answer,
//but it's here where we tell Spring to load this filter at the right point in the chain
//(with an order of precedence higher than oauth2's filters)
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return bean;
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// auth
// .inMemoryAuthentication()
// .withUser("user").password("{noop}pass").roles("USER");
auth.userDetailsService(securityUserDetailsService).passwordEncoder(passwordEncoder());
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/example/demo/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("http://localhost:8080");
}
}
1 change: 0 additions & 1 deletion src/main/java/com/example/demo/post/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import java.util.List;

@CrossOrigin("*")
@RestController
@RequestMapping("/api")
public class PostController {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/example/demo/user/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
public class Member {
@Id
private String memberId;
@Column(length = 60)
private String memberPass;
private String memberName;
private String memberTel;
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/example/demo/user/SecurityUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;

import java.util.List;

public class SecurityUser extends User {
public SecurityUser(Member member) {
super(member.getMemberId(), member.getMemberPass(), AuthorityUtils.createAuthorityList(member.getRole()));
public SecurityUser(String username, String password, List role) {
super(username, password, role);
}
}
12 changes: 7 additions & 5 deletions src/main/java/com/example/demo/user/SecurityUserContoller.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Controller
@RestController
public class SecurityUserContoller {
@Autowired
SecurityUserDetailsService securityUserDetailsService;


@GetMapping("/signup")
public String showSignUp() {
return "signUp";
}

@PostMapping("signup")
public String signUp(Member member) {
@PostMapping("/signup")
public int signUp(@RequestBody Member member) {
securityUserDetailsService.securitySave(member);
return "home";
System.out.println("회원가입 완료");
return 200;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
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.User;
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 java.util.ArrayList;
import java.util.List;

@Service
public class SecurityUserDetailsService implements UserDetailsService {
Expand All @@ -25,7 +25,7 @@ public class SecurityUserDetailsService implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Member member = memberRepositoy.findById(username).get();
return new SecurityUser(member);
return new User(member.getMemberId(), member.getMemberPass(), new ArrayList<>());
}

public void securitySave(Member member) {
Expand Down
4 changes: 0 additions & 4 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ spring:
enabled: true
freemarker:
cache: false
security:
# security
user:
name: user
passwrd: test


# change server port
Expand Down

0 comments on commit 0ec2d69

Please sign in to comment.