security order set
This commit is contained in:
@@ -3,6 +3,7 @@ package io.bluemoon.authorizationserver.config;
|
||||
import io.bluemoon.authorizationserver.config.handler.CustomAuthFailureHandler;
|
||||
import io.bluemoon.authorizationserver.service.user.CustomUserDetailsServiceImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.security.SecurityProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
@@ -23,7 +24,7 @@ import org.springframework.web.filter.CharacterEncodingFilter;
|
||||
@Configuration
|
||||
//@EnableOAuth2Client
|
||||
//@Order(SecurityProperties.BASIC_AUTH_ORDER - 6)
|
||||
@Order(-1)
|
||||
@Order(SecurityProperties.DEFAULT_FILTER_ORDER)
|
||||
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
|
||||
@Autowired
|
||||
|
||||
@@ -25,13 +25,11 @@ ext {
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
implementation 'org.springframework.cloud:spring-cloud-starter-oauth2'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
|
||||
implementation 'com.squareup.okhttp3:okhttp:3.9.1'
|
||||
implementation 'com.google.code.gson:gson'
|
||||
compileOnly 'org.projectlombok:lombok'
|
||||
runtimeOnly 'org.postgresql:postgresql'
|
||||
annotationProcessor 'org.projectlombok:lombok'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
||||
@@ -4,7 +4,12 @@ import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
|
||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
@@ -14,6 +19,7 @@ import org.springframework.web.bind.annotation.SessionAttributes;
|
||||
import java.security.Principal;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableResourceServer
|
||||
public class TestServiceApplication {
|
||||
|
||||
|
||||
@@ -21,28 +27,39 @@ public class TestServiceApplication {
|
||||
SpringApplication.run(TestServiceApplication.class, args);
|
||||
}
|
||||
|
||||
// @Controller
|
||||
// @RequestMapping("/")
|
||||
// public static class TestController{
|
||||
//
|
||||
// @RequestMapping(method = RequestMethod.GET)
|
||||
// @ResponseBody
|
||||
// public String helloMk2(Principal principal) {
|
||||
// return principal == null ? "hello anonymous" : "heelo" + principal.getName();
|
||||
// }
|
||||
//
|
||||
// @PreAuthorize("#oauth2.hasScope('read') and hasRole('ROLE_USER')")
|
||||
// @RequestMapping(value = "secret", method = RequestMethod.GET)
|
||||
// @ResponseBody
|
||||
// public String helloMk2Secret(Principal principal) {
|
||||
// return principal == null ? "hello anonymous" : "heelo" + principal.getName();
|
||||
// }
|
||||
//
|
||||
// @RequestMapping(method = RequestMethod.GET, value = "test")
|
||||
// @ResponseBody
|
||||
// public String test() {
|
||||
// return "test";
|
||||
// }
|
||||
// }
|
||||
@Controller
|
||||
@RequestMapping("/")
|
||||
public static class TestController{
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public String helloMk2(Principal principal) {
|
||||
|
||||
System.out.println("-------------");
|
||||
System.out.println(SecurityContextHolder.getContext().getAuthentication().getName());
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
// UserDetails userDetails = (UserDetails) authentication.getPrincipal();
|
||||
OAuth2Authentication oAuth2Authentication = (OAuth2Authentication)authentication;
|
||||
OAuth2AuthenticationDetails oAuth2AuthenticationDetails = (OAuth2AuthenticationDetails) oAuth2Authentication.getDetails();
|
||||
|
||||
// System.out.println(userDetails.getUsername());
|
||||
System.out.println(oAuth2AuthenticationDetails.getTokenValue());
|
||||
System.out.println("-------------");
|
||||
return principal == null ? "hello anonymous" : "heelo" + principal.getName();
|
||||
}
|
||||
|
||||
@PreAuthorize("#oauth2.hasScope('read') and hasRole('ROLE_USER')")
|
||||
@RequestMapping(value = "secret", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public String helloMk2Secret(Principal principal) {
|
||||
return principal == null ? "hello anonymous" : "heelo" + principal.getName();
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "test")
|
||||
@ResponseBody
|
||||
public String test() {
|
||||
return "test";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
package io.bluemoon.testservice.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
|
||||
|
||||
@Configuration
|
||||
@EnableResourceServer
|
||||
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
|
||||
|
||||
|
||||
@Override
|
||||
public void configure(HttpSecurity http) throws Exception {
|
||||
http
|
||||
.authorizeRequests()
|
||||
.antMatchers("/signIn").permitAll()
|
||||
.antMatchers("/signUp").permitAll()
|
||||
.antMatchers("/signInMiddleWare").permitAll()
|
||||
.antMatchers("/signUpMiddleWare").permitAll()
|
||||
.anyRequest()
|
||||
.authenticated();
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package io.bluemoon.testservice.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@EnableWebMvc
|
||||
@Configuration
|
||||
public class WebMvcConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
registry.addViewController("/signIn").setViewName("signIn");
|
||||
registry.addViewController("/signUp").setViewName("signUp");
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package io.bluemoon.testservice.config;
|
||||
|
||||
import io.bluemoon.testservice.handler.CustomAuthFailureHandler;
|
||||
import org.springframework.boot.autoconfigure.security.SecurityProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
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
|
||||
//@Order(-1)
|
||||
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http
|
||||
.authorizeRequests()
|
||||
.antMatchers("/signIn").permitAll()
|
||||
.antMatchers("/signUp").permitAll()
|
||||
.antMatchers("/signInMiddleWare").permitAll()
|
||||
.antMatchers("/signUpMiddleWare").permitAll()
|
||||
.anyRequest()
|
||||
.authenticated();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
package io.bluemoon.testservice.controller;
|
||||
|
||||
import io.bluemoon.testservice.domain.user.User;
|
||||
import io.bluemoon.testservice.service.user.UserService;
|
||||
import org.springframework.boot.autoconfigure.security.SecurityProperties;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.validation.Valid;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
public class SignController {
|
||||
|
||||
private UserService userService;
|
||||
|
||||
public SignController(
|
||||
UserService userService
|
||||
) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@PostMapping("/signInMiddleWare")
|
||||
public Map signInMiddleWare(HttpServletRequest request) throws IOException {
|
||||
User user = requestToUser(request);
|
||||
|
||||
return userService.readUser(user);
|
||||
}
|
||||
|
||||
@PostMapping("/signUpMiddleWare")
|
||||
public User signUpMiddleWare(HttpServletRequest request) {
|
||||
|
||||
User user = requestToUser(request);
|
||||
|
||||
return userService.createUser(user);
|
||||
|
||||
}
|
||||
|
||||
@PostMapping("/createOAuthUser")
|
||||
public String creatOAuthUser(@RequestBody @Valid User user, @RequestHeader Map header) {
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
private User requestToUser(HttpServletRequest request) {
|
||||
User user = new User();
|
||||
user.setUsername(request.getParameter("username"));
|
||||
user.setPassword(request.getParameter("password"));
|
||||
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package io.bluemoon.testservice.domain.oauth;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collection;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
@Table(name = "oauth_user", schema = "oauth2")
|
||||
public class OAuthUser {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private int id;
|
||||
|
||||
@Column
|
||||
private String username;
|
||||
|
||||
@Column
|
||||
private String password;
|
||||
|
||||
@Column
|
||||
private String name;
|
||||
|
||||
@Column
|
||||
private String email;
|
||||
|
||||
@Column
|
||||
private String ResourceId;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, mappedBy = "oAuthUser", fetch = FetchType.EAGER)
|
||||
private Collection<OAuthUserRole> userRole;
|
||||
|
||||
@Column
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
@Column
|
||||
private LocalDateTime updatedAt;
|
||||
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package io.bluemoon.testservice.domain.oauth;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface OAuthUserRepository extends JpaRepository<OAuthUser, Integer> {
|
||||
|
||||
Optional<OAuthUser> findByUsername(String username);
|
||||
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package io.bluemoon.testservice.domain.oauth;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
@ToString(exclude = "oAuthUser")
|
||||
@Table(name = "oauth_user_role", schema = "oauth2")
|
||||
public class OAuthUserRole {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private int id;
|
||||
|
||||
@Column
|
||||
private String role;
|
||||
|
||||
@Column
|
||||
private String projectId;
|
||||
|
||||
@ManyToOne(optional = false)
|
||||
@JoinColumn(name = "userId")
|
||||
private OAuthUser oAuthUser;
|
||||
|
||||
@Column
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
@Column
|
||||
private LocalDateTime updatedAt;
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package io.bluemoon.testservice.domain.oauth;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface OAuthUserRoleRepository extends JpaRepository<OAuthUserRole, Integer> {
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package io.bluemoon.testservice.domain.user;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
@Table(name = "user", schema = "service")
|
||||
public class User {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private int id;
|
||||
|
||||
@Column
|
||||
private String username;
|
||||
@Column
|
||||
private String password;
|
||||
@Column
|
||||
private String status;
|
||||
@Column
|
||||
private LocalDateTime createdAt;
|
||||
@Column
|
||||
private LocalDateTime updatedAt;
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package io.bluemoon.testservice.domain.user;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Integer> {
|
||||
|
||||
Optional<User> findByUsername(String username);
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package io.bluemoon.testservice.handler;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class CustomAuthFailureHandler implements AuthenticationFailureHandler {
|
||||
@Override
|
||||
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
|
||||
// logging 붙이기
|
||||
System.out.println(exception.getMessage());
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package io.bluemoon.testservice.service.oauth;
|
||||
|
||||
import io.bluemoon.testservice.domain.oauth.OAuthUser;
|
||||
import io.bluemoon.testservice.domain.user.User;
|
||||
|
||||
public interface OAuthUserService {
|
||||
|
||||
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package io.bluemoon.testservice.service.oauth;
|
||||
|
||||
import io.bluemoon.testservice.domain.oauth.OAuthUser;
|
||||
import io.bluemoon.testservice.domain.oauth.OAuthUserRepository;
|
||||
import io.bluemoon.testservice.domain.oauth.OAuthUserRoleRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class OAuthUserServiceImpl implements OAuthUserService {
|
||||
|
||||
private OAuthUserRepository oAuthUserRepository;
|
||||
private OAuthUserRoleRepository oAuthUserRoleRepository;
|
||||
|
||||
public OAuthUserServiceImpl(
|
||||
OAuthUserRepository oAuthUserRepository,
|
||||
OAuthUserRoleRepository oAuthUserRoleRepository
|
||||
) {
|
||||
this.oAuthUserRepository = oAuthUserRepository;
|
||||
this.oAuthUserRoleRepository = oAuthUserRoleRepository;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package io.bluemoon.testservice.service.user;
|
||||
|
||||
import io.bluemoon.testservice.domain.oauth.OAuthUser;
|
||||
import io.bluemoon.testservice.domain.oauth.OAuthUserRepository;
|
||||
import io.bluemoon.testservice.domain.user.User;
|
||||
import io.bluemoon.testservice.service.oauth.OAuthUserService;
|
||||
import io.bluemoon.testservice.utils.APIRequest;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.event.TransactionPhase;
|
||||
import org.springframework.transaction.event.TransactionalEventListener;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Component
|
||||
public class UserEventListener {
|
||||
|
||||
// private OAuthUserService oAuthUserService;
|
||||
private OAuthUserRepository oAuthUserRepository;
|
||||
|
||||
public UserEventListener(
|
||||
OAuthUserRepository oAuthUserRepository
|
||||
) {
|
||||
this.oAuthUserRepository = oAuthUserRepository;
|
||||
}
|
||||
|
||||
@Async
|
||||
@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT, classes = UserServiceImpl.UserCreateEvent.class)
|
||||
public void userCreateEventHandle(UserServiceImpl.UserCreateEvent event) throws IOException {
|
||||
User user = event.getUser();
|
||||
System.out.println("----------handler");
|
||||
System.out.println(user);
|
||||
OAuthUser oAuthUser = new OAuthUser();
|
||||
oAuthUser.setUsername(user.getUsername());
|
||||
oAuthUser.setPassword(user.getPassword());
|
||||
|
||||
oAuthUserRepository.saveAndFlush(oAuthUser);
|
||||
|
||||
|
||||
|
||||
// APIRequest.ResponseWrapper responseWrapper = APIRequest.getIRequestExecutor().createOAuthUser(user);
|
||||
// System.out.println(responseWrapper.getHeader());
|
||||
// System.out.println(responseWrapper.getBody());
|
||||
|
||||
// oauth
|
||||
}
|
||||
|
||||
@Async
|
||||
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT, classes = UserServiceImpl.UserUpdateEvent.class)
|
||||
public void userUpdateEventHandle(UserServiceImpl.UserUpdateEvent event) throws IOException {
|
||||
System.out.println("----------handler");
|
||||
User user = event.getUser();
|
||||
// oauth
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package io.bluemoon.testservice.service.user;
|
||||
|
||||
import io.bluemoon.testservice.domain.user.User;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
public interface UserService {
|
||||
|
||||
User createUser(User user);
|
||||
|
||||
User updateUser(User user);
|
||||
|
||||
Map readUser(User user) throws IOException;
|
||||
}
|
||||
@@ -1,136 +0,0 @@
|
||||
package io.bluemoon.testservice.service.user;
|
||||
|
||||
import io.bluemoon.testservice.domain.oauth.OAuthUser;
|
||||
import io.bluemoon.testservice.domain.oauth.OAuthUserRepository;
|
||||
import io.bluemoon.testservice.domain.user.User;
|
||||
import io.bluemoon.testservice.domain.user.UserRepository;
|
||||
import io.bluemoon.testservice.service.oauth.OAuthUserService;
|
||||
import io.bluemoon.testservice.utils.APIRequest;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.security.core.parameters.P;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class UserServiceImpl implements UserService, ApplicationEventPublisherAware {
|
||||
|
||||
private UserRepository userRepository;
|
||||
private ApplicationEventPublisher eventPublisher;
|
||||
private OAuthUserRepository oAuthUserRepository;
|
||||
|
||||
public UserServiceImpl(
|
||||
UserRepository userRepository,
|
||||
OAuthUserRepository oAuthUserRepository
|
||||
) {
|
||||
this.userRepository = userRepository;
|
||||
this.oAuthUserRepository = oAuthUserRepository;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public User createUser(User user) {
|
||||
System.out.println(passwordEncoder().encode(user.getPassword()));
|
||||
user.setPassword(passwordEncoder().encode(user.getPassword()));
|
||||
System.out.println(passwordEncoder().matches("1234", user.getPassword()));
|
||||
userRepository.save(user);
|
||||
// OAuthUser o = new OAuthUser();
|
||||
// o.setPassword("123");
|
||||
// o.setName("tomz");
|
||||
// oAuthUserRepository.save(o);
|
||||
eventPublisher.publishEvent(new UserCreateEvent(user));
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map readUser(User user) throws IOException {
|
||||
System.out.println(user);
|
||||
|
||||
|
||||
Optional<User> optionalUser =
|
||||
userRepository.findByUsername(user.getUsername());
|
||||
|
||||
if (optionalUser.isPresent()) {
|
||||
System.out.println(optionalUser.get().toString());
|
||||
if (passwordEncoder().matches(user.getPassword(),optionalUser.get().getPassword())) {
|
||||
|
||||
Optional<OAuthUser> optionalOAuthUser = oAuthUserRepository.findByUsername(user.getUsername());
|
||||
if (passwordEncoder().matches(user.getPassword(), optionalOAuthUser.get().getPassword())) {
|
||||
// client id
|
||||
Map<String, Object> tokenInfo = new HashMap<>();
|
||||
tokenInfo.put("username", user.getUsername());
|
||||
tokenInfo.put("password", user.getPassword());
|
||||
tokenInfo.put("grant_type", "password");
|
||||
APIRequest.ResponseWrapper response = APIRequest.getIRequestExecutor().createOAuthToken(tokenInfo);
|
||||
System.out.println();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
// update password는 별로의 로직으로?
|
||||
@Override
|
||||
public User updateUser(User user) {
|
||||
Optional<User> optionalUser = userRepository.findById(user.getId());
|
||||
if (optionalUser.isPresent()) {
|
||||
user.setId(optionalUser.get().getId());
|
||||
userRepository.save(user);
|
||||
eventPublisher.publishEvent(new UserUpdateEvent(user));
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
||||
this.eventPublisher = applicationEventPublisher;
|
||||
}
|
||||
|
||||
public static class UserCreateEvent {
|
||||
@Getter
|
||||
private User user;
|
||||
|
||||
private UserCreateEvent(@NonNull User user) {
|
||||
this.user = user;
|
||||
}
|
||||
}
|
||||
|
||||
public static class UserReadEvent {
|
||||
@Getter
|
||||
private User user;
|
||||
|
||||
private UserReadEvent(@NonNull User user) {
|
||||
this.user = user;
|
||||
}
|
||||
}
|
||||
|
||||
public static class UserUpdateEvent {
|
||||
@Getter
|
||||
private User user;
|
||||
|
||||
private UserUpdateEvent(@NonNull User user) {
|
||||
this.user = user;
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PasswordEncoder passwordEncoder() {
|
||||
return new BCryptPasswordEncoder();
|
||||
}
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
package io.bluemoon.testservice.utils;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import io.bluemoon.testservice.domain.user.User;
|
||||
import lombok.Getter;
|
||||
import okhttp3.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class APIRequest {
|
||||
|
||||
private static IRequestExecutor iRequestExecutor = new DefaultRequestExecutor();
|
||||
|
||||
public static IRequestExecutor getIRequestExecutor() {
|
||||
return iRequestExecutor;
|
||||
}
|
||||
|
||||
public interface IRequestExecutor {
|
||||
ResponseWrapper createOAuthToken(Map tokenInfo) throws IOException;
|
||||
ResponseWrapper updateOAuthUser(User user);
|
||||
|
||||
ResponseWrapper createOAuthClientDetails();
|
||||
ResponseWrapper updateOAuthClientDetials();
|
||||
}
|
||||
|
||||
public static class DefaultRequestExecutor implements IRequestExecutor {
|
||||
static okhttp3.OkHttpClient client = null;
|
||||
static void init() {
|
||||
client = new okhttp3.OkHttpClient();
|
||||
}
|
||||
static {
|
||||
init();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public ResponseWrapper createOAuthToken(Map tokenInfo) throws IOException {
|
||||
String url = "http://localhost:8081/auth/oauth/token";
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
String jsonString = gson.toJson(tokenInfo);
|
||||
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonString);
|
||||
Request request = new Request.Builder()
|
||||
.url(url)
|
||||
.addHeader("Authorization", Credentials.basic("a","1"))
|
||||
.post(body)
|
||||
.header("Content-type", "application/json")
|
||||
.build();
|
||||
|
||||
Call call = client.newCall(request);
|
||||
Response response = call.execute();
|
||||
ResponseWrapper result = new ResponseWrapper(response.body().string(), convertToString(response.headers()));
|
||||
System.out.println("----------===================------------");
|
||||
System.out.println(result.getBody());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseWrapper updateOAuthUser(User user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseWrapper createOAuthClientDetails() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseWrapper updateOAuthClientDetials() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static String convertToString(Object input) {
|
||||
if (input == null) {
|
||||
return "null";
|
||||
} else if (input instanceof Map) {
|
||||
Gson gson = new GsonBuilder()
|
||||
.excludeFieldsWithModifiers(Modifier.STATIC)
|
||||
.excludeFieldsWithModifiers(Modifier.PROTECTED)
|
||||
.disableHtmlEscaping()
|
||||
.create();
|
||||
return gson.toJson((Map)input);
|
||||
} else if (input instanceof List) {
|
||||
Gson gson = new GsonBuilder()
|
||||
.excludeFieldsWithModifiers(Modifier.STATIC)
|
||||
.excludeFieldsWithModifiers(Modifier.PROTECTED)
|
||||
.disableHtmlEscaping()
|
||||
.create();
|
||||
return gson.toJson((List)input);
|
||||
} else {
|
||||
return input.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Getter
|
||||
public static class ResponseWrapper {
|
||||
private String body;
|
||||
private String header;
|
||||
|
||||
public ResponseWrapper(String body, String header) {
|
||||
this.body = body;
|
||||
this.header = header;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package io.bluemoon.testservice.utils;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Getter
|
||||
@Setter
|
||||
public class UserContext {
|
||||
public static final String CORREATION_ID = "tmx-correlation-id";
|
||||
public static final String AUTH_TOKEN = "Authorization";
|
||||
public static final String USER_ID = "tmx-user-id";
|
||||
public static final String ORG_ID = "tmx-org-id";
|
||||
|
||||
private String correlationId;
|
||||
private String authToken;
|
||||
private String userId;
|
||||
private String orgId;
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package io.bluemoon.testservice.utils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class UserContextFilter implements Filter {
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
||||
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
|
||||
log.debug("I am entering the licensing service id with auth token : ", httpServletRequest.getHeader("Authorization"));
|
||||
|
||||
UserContextHolder.getContext().setCorrelationId(httpServletRequest.getHeader(UserContext.CORREATION_ID));
|
||||
UserContextHolder.getContext().setUserId(httpServletRequest.getHeader(UserContext.USER_ID));
|
||||
UserContextHolder.getContext().setAuthToken(httpServletRequest.getHeader(UserContext.AUTH_TOKEN));
|
||||
UserContextHolder.getContext().setOrgId(httpServletRequest.getHeader(UserContext.ORG_ID));
|
||||
|
||||
chain.doFilter(httpServletRequest, response);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package io.bluemoon.testservice.utils;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
public class UserContextHolder {
|
||||
private static final ThreadLocal<UserContext> userContext = new ThreadLocal<UserContext>();
|
||||
|
||||
public static final UserContext getContext() {
|
||||
UserContext context = userContext.get();
|
||||
|
||||
if (context == null) {
|
||||
context = createEmptyContext();
|
||||
userContext.set(context);
|
||||
}
|
||||
return userContext.get();
|
||||
}
|
||||
|
||||
public static final void setContext(UserContext context) {
|
||||
Assert.notNull(context, "Only non-null UserContext instances are permitted");
|
||||
userContext.set(context);
|
||||
}
|
||||
|
||||
public static final UserContext createEmptyContext() {
|
||||
return new UserContext();
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package io.bluemoon.testservice.utils;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class UserContextInterceptor implements ClientHttpRequestInterceptor {
|
||||
@Override
|
||||
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
|
||||
|
||||
HttpHeaders headers = request.getHeaders();
|
||||
headers.add(UserContext.CORREATION_ID, UserContextHolder.getContext().getCorrelationId());
|
||||
headers.add(UserContext.AUTH_TOKEN, UserContextHolder.getContext().getAuthToken());
|
||||
|
||||
return execution.execute(request, body);
|
||||
}
|
||||
}
|
||||
@@ -1,33 +1,9 @@
|
||||
server.port=8082
|
||||
server.servlet.context-path=/api
|
||||
#server.servlet.context-path=/api
|
||||
|
||||
#security.oauth2.resource.jwt.key-value="abc"
|
||||
#security.oauth2.resource.id=read
|
||||
#security.oauth2.resource.service-id=${PREFIX:}resource
|
||||
security.oauth2.resource.user-info-uri=http://localhost:8081/uaa/user
|
||||
security.oauth2.resource.prefer-token-info=false
|
||||
|
||||
#security.oauth2.client.client-id=system1
|
||||
#security.oauth2.client.client-secret=1234
|
||||
#security.oauth2.resource.token-info-uri=http://127.0.0.1:8081/mk-auth/oauth/check_token
|
||||
security.oauth2.resource.user-info-uri=http://127.0.0.1:8765/auth/check_token
|
||||
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
#spring.datasource.url=jdbc:mysql://127.0.0.1/local_keepgrow?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC
|
||||
#spring.datasource.username=root
|
||||
#spring.datasource.password=bluemoon
|
||||
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
#spring.datasource.platform=schema
|
||||
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDB53Dialect
|
||||
|
||||
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false
|
||||
spring.datasource.url=jdbc:postgresql://localhost:5432/mk2
|
||||
#spring.datasource.platform=postgres
|
||||
#spring.datasource.driver-class-name=org.postgresql.Driver
|
||||
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
|
||||
|
||||
spring.jpa.database = POSTGRESQL
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.jpa.generate-ddl=true
|
||||
spring.jpa.show-sql=true
|
||||
|
||||
logging.level.web=debug
|
||||
spring.http.log-request-details=true
|
||||
Reference in New Issue
Block a user