diff --git a/authorization-server2/build.gradle b/authorization-server2/build.gradle new file mode 100644 index 0000000..3a21ca2 --- /dev/null +++ b/authorization-server2/build.gradle @@ -0,0 +1,46 @@ +plugins { + id 'org.springframework.boot' version '2.1.5.RELEASE' + id 'java' +} + +apply plugin: 'io.spring.dependency-management' + +group = 'io.bluemoon' +version = '0.0.1-SNAPSHOT' +sourceCompatibility = '1.8' + +configurations { + compileOnly { + extendsFrom annotationProcessor + } +} + +repositories { + mavenCentral() +} + +ext { + set('springCloudVersion', 'Greenwich.SR1') +} + +dependencies { + compile('org.springframework.security:spring-security-oauth2-client') + compile('org.springframework.security:spring-security-oauth2-jose') + implementation 'org.springframework.boot:spring-boot-starter-actuator' + implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' + implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.cloud:spring-cloud-starter-oauth2' +// implementation 'org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.5.RELEASE' + implementation 'org.springframework.cloud:spring-cloud-starter-security' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + compileOnly 'org.projectlombok:lombok' + runtimeOnly 'mysql:mysql-connector-java' + annotationProcessor 'org.projectlombok:lombok' + testImplementation 'org.springframework.boot:spring-boot-starter-test' +} + +dependencyManagement { + imports { + mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" + } +} diff --git a/authorization-server2/gradle/wrapper/gradle-wrapper.jar b/authorization-server2/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..87b738c Binary files /dev/null and b/authorization-server2/gradle/wrapper/gradle-wrapper.jar differ diff --git a/authorization-server2/gradle/wrapper/gradle-wrapper.properties b/authorization-server2/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..809a2c6 --- /dev/null +++ b/authorization-server2/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Fri Jul 05 17:06:35 KST 2019 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip diff --git a/authorization-server2/gradlew b/authorization-server2/gradlew new file mode 100755 index 0000000..af6708f --- /dev/null +++ b/authorization-server2/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/authorization-server2/gradlew.bat b/authorization-server2/gradlew.bat new file mode 100644 index 0000000..0f8d593 --- /dev/null +++ b/authorization-server2/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/authorization-server2/settings.gradle b/authorization-server2/settings.gradle new file mode 100644 index 0000000..0acbfb5 --- /dev/null +++ b/authorization-server2/settings.gradle @@ -0,0 +1,6 @@ +pluginManagement { + repositories { + gradlePluginPortal() + } +} +rootProject.name = 'authorization-server2' diff --git a/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/AuthorizationServer2Application.java b/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/AuthorizationServer2Application.java new file mode 100644 index 0000000..3c2fbbd --- /dev/null +++ b/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/AuthorizationServer2Application.java @@ -0,0 +1,13 @@ +package io.bluemoon.authorizationserver2; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class AuthorizationServer2Application { + + public static void main(String[] args) { + SpringApplication.run(AuthorizationServer2Application.class, args); + } + +} diff --git a/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/config/AuthorizationServer2Config.java b/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/config/AuthorizationServer2Config.java new file mode 100644 index 0000000..3af4b38 --- /dev/null +++ b/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/config/AuthorizationServer2Config.java @@ -0,0 +1,101 @@ +package io.bluemoon.authorizationserver2.config; + +import io.bluemoon.authorizationserver2.service.user.CustomUserDetailsServiceImpl; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; +import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; +import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; +import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer; +import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer; +import org.springframework.security.oauth2.provider.ClientDetailsService; +import org.springframework.security.oauth2.provider.approval.ApprovalStore; +import org.springframework.security.oauth2.provider.approval.JdbcApprovalStore; +import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService; +import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices; +import org.springframework.security.oauth2.provider.code.JdbcAuthorizationCodeServices; +import org.springframework.security.oauth2.provider.token.TokenStore; +import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore; + +import javax.sql.DataSource; + +@Configuration +@EnableAuthorizationServer +public class AuthorizationServer2Config extends AuthorizationServerConfigurerAdapter { + + @Autowired + private ApprovalStore approvalStore; + + private ClientDetailsService clientDetailsService; + private AuthenticationManager authenticationManager; + private DataSource dataSource; + private CustomUserDetailsServiceImpl customUserDetailsService; + + public AuthorizationServer2Config( + ClientDetailsService clientDetailsService, + AuthenticationManager authenticationManager, + DataSource dataSource, + CustomUserDetailsServiceImpl customUserDetailsService + ) { + this.clientDetailsService = clientDetailsService; + this.authenticationManager = authenticationManager; + this.dataSource = dataSource; + this.customUserDetailsService = customUserDetailsService; + } + + @Override + public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { + security.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()"); + } + + @Override + public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { + endpoints + // authentication + // 비밀 번호 부여는 AuthenticationManager를 주입해야 켜짐 + .authenticationManager(authenticationManager) + // jdbc token processing + .tokenStore(jdbcTokenStore(dataSource)) + + // 사용자 세부 정보가 필요할 때 + .userDetailsService(customUserDetailsService) + // approval store + .approvalStore(approvalStore) + // refresh token + .reuseRefreshTokens(true); + + } + + @Override + public void configure(ClientDetailsServiceConfigurer clients) throws Exception { + clients.withClientDetails(clientDetailsService); + } + + @Bean + public TokenStore jdbcTokenStore(DataSource dataSource) { + return new JdbcTokenStore(dataSource); + } + + @Bean + @Primary + public JdbcClientDetailsService jdbcClientDetailsService(DataSource dataSource) { + return new JdbcClientDetailsService(dataSource); + } + +// @Bean +// public AuthorizationCodeServices jdbcAuthorizationCodeServies(DataSource dataSource) { +// return new JdbcAuthorizationCodeServices(dataSource); +// } + + @Bean + public ApprovalStore jdbcApprovalStore(DataSource dataSource) { + return new JdbcApprovalStore(dataSource); + } + // jwt + + + +} diff --git a/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/config/WebSecurity2Config.java b/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/config/WebSecurity2Config.java new file mode 100644 index 0000000..28779bc --- /dev/null +++ b/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/config/WebSecurity2Config.java @@ -0,0 +1,53 @@ +package io.bluemoon.authorizationserver2.config; + +import io.bluemoon.authorizationserver2.service.user.CustomUserDetailsServiceImpl; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.dao.DaoAuthenticationProvider; +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.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.crypto.password.NoOpPasswordEncoder; + +@Configuration +public class WebSecurity2Config extends WebSecurityConfigurerAdapter { + private CustomUserDetailsServiceImpl customUserDetailsService; + + public WebSecurity2Config( + CustomUserDetailsServiceImpl customUserDetailsService + ) { + this.customUserDetailsService = customUserDetailsService; + } + + @Bean + @Override + public AuthenticationManager authenticationManagerBean() throws Exception { + return super.authenticationManagerBean(); + } + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.authenticationProvider(daoAuthenticationProvider()); + } + + @Override + protected void configure(HttpSecurity http) throws Exception { + super.configure(http); + } + + @Bean + public DaoAuthenticationProvider daoAuthenticationProvider() { + DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider(); + daoAuthenticationProvider.setUserDetailsService(customUserDetailsService); + daoAuthenticationProvider.setPasswordEncoder(passwordEncoder()); + return daoAuthenticationProvider; + } + + // 패스워드 인코딩 수정 + @Bean + @SuppressWarnings("deprecation") + public static NoOpPasswordEncoder passwordEncoder() { + return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance(); + } +} diff --git a/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/controller/AuthController.java b/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/controller/AuthController.java new file mode 100644 index 0000000..c7f6176 --- /dev/null +++ b/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/controller/AuthController.java @@ -0,0 +1,15 @@ +package io.bluemoon.authorizationserver2.controller; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.security.Principal; + +@RestController +public class AuthController { + @RequestMapping(value = "/user") + public Principal getUser(Principal user) { + return user; + } + +} diff --git a/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/domain/user/CustomUserDetails.java b/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/domain/user/CustomUserDetails.java new file mode 100644 index 0000000..25cf81b --- /dev/null +++ b/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/domain/user/CustomUserDetails.java @@ -0,0 +1,56 @@ +package io.bluemoon.authorizationserver2.domain.user; + +import lombok.Data; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +@Data +public class CustomUserDetails implements UserDetails { + + private static final long serialVersionUID = 6396079419309274853L; + private Long id; + private String username; + private String password; + private List userRole; + + public CustomUserDetails(User user, List userRole) { + this.id = user.getId(); + this.username = user.getUsername(); + this.password = user.getPassword(); + this.userRole = userRole; + } + + @Override + public Collection getAuthorities() { + List authorities = new ArrayList<>(); + for (String role: userRole) { + authorities.add(new SimpleGrantedAuthority(role)); + } + return authorities; + } + + @Override + public boolean isAccountNonExpired() { + return true; + } + + @Override + public boolean isAccountNonLocked() { + return true; + } + + @Override + public boolean isCredentialsNonExpired() { + return true; + } + + @Override + public boolean isEnabled() { + return true; + } +} diff --git a/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/domain/user/User.java b/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/domain/user/User.java new file mode 100644 index 0000000..712c372 --- /dev/null +++ b/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/domain/user/User.java @@ -0,0 +1,69 @@ +package io.bluemoon.authorizationserver2.domain.user; + +import io.bluemoon.authorizationserver.domain.social.SocialType; +import lombok.*; + +import javax.persistence.*; +import java.time.LocalDateTime; +import java.util.Collection; + +@Entity +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class User { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column + private String username; + + @Column + private String name; + + @Column + private String password; + + @Column + private String email; + + @Column + private String principal; + + @Column + @Enumerated(EnumType.STRING) + private SocialType socialType; + + @OneToMany(cascade = CascadeType.ALL, mappedBy = "user", fetch = FetchType.EAGER) + private Collection userRole; + + @Column + private LocalDateTime createdAt; + + @Column + private LocalDateTime updatedAt; + + + +// //1:수퍼관리자, 2:관리자, 3:사용자 +// @Column +// private String userType; + +// @Column +// @Temporal(TemporalType.TIMESTAMP) +// private Date regDate = new Date(); + + @Builder + public User(String username, String name, String password, String email, String principal, LocalDateTime createdAt, LocalDateTime updatedAt, SocialType socialType) { + this.username = username; + this.name = name; + this.password = password; + this.email = email; + this.principal = principal; + this.createdAt = createdAt; + this.updatedAt = updatedAt; + this.socialType = socialType; + } +} diff --git a/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/domain/user/UserRepository.java b/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/domain/user/UserRepository.java new file mode 100644 index 0000000..a04b954 --- /dev/null +++ b/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/domain/user/UserRepository.java @@ -0,0 +1,8 @@ +package io.bluemoon.authorizationserver2.domain.user; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserRepository extends JpaRepository { + User findByUsername(String username); + User findByEmail(String email); +} diff --git a/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/domain/user/UserRole.java b/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/domain/user/UserRole.java new file mode 100644 index 0000000..e6c73ec --- /dev/null +++ b/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/domain/user/UserRole.java @@ -0,0 +1,30 @@ +package io.bluemoon.authorizationserver2.domain.user; + +import lombok.*; + +import javax.persistence.*; + +@Getter +@Setter +@Entity +@ToString(exclude = "user") +@NoArgsConstructor +@AllArgsConstructor +public class UserRole { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column + private String role; + + @ManyToOne(optional = false) + @JoinColumn(name = "userId") + private User user; + + @Builder + public UserRole(User user, String role) { + this.user = user; + this.role = role; + } +} diff --git a/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/domain/user/UserRoleRepository.java b/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/domain/user/UserRoleRepository.java new file mode 100644 index 0000000..f1680e0 --- /dev/null +++ b/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/domain/user/UserRoleRepository.java @@ -0,0 +1,10 @@ +package io.bluemoon.authorizationserver2.domain.user; + +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface UserRoleRepository extends JpaRepository { + List findByUser(User user); + +} diff --git a/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/service/user/CustomUserDetailsServiceImpl.java b/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/service/user/CustomUserDetailsServiceImpl.java new file mode 100644 index 0000000..489256f --- /dev/null +++ b/authorization-server2/src/main/java/io/bluemoon/authorizationserver2/service/user/CustomUserDetailsServiceImpl.java @@ -0,0 +1,65 @@ +package io.bluemoon.authorizationserver2.service.user; + +import io.bluemoon.authorizationserver2.domain.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.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +public class CustomUserDetailsServiceImpl implements UserDetailsService { + + // User Info + private UserRepository userRepository; + private UserRoleRepository userRoleRepository; + + public CustomUserDetailsServiceImpl( + UserRepository userRepository, + UserRoleRepository userRoleRepository + ) { + this.userRepository = userRepository; + this.userRoleRepository = userRoleRepository; + } + + @Override + public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { + System.out.println("why?????????????"+username); + User user = userRepository.findByUsername(username); + System.out.println(user); + List userRole = userRoleRepository.findByUser(user); + System.out.println(userRole); + System.out.println("---------------------------"); + List urs = new ArrayList<>(); + for (UserRole ur : userRole) { + urs.add(ur.getRole()); + } + + if (user == null) { + throw new UsernameNotFoundException("UsernameNotFound[" + username + "]"); + } + + CustomUserDetails userDetail = new CustomUserDetails(user, urs); + System.out.println(userDetail); + return userDetail; + } + + /** + * User role check + * @param user + * @param userRole + * @return + */ +// private CustomUserDetails createUser(User user, List userRole) { +// CustomUserDetails userDetail = +// +//// if (userDetail.getSocial_type().getVaule().equals("FACEBOOK")) { +//// userDetail.setRoles(Arrays.asList("ROLE_FACEBOOK")); +//// } else { +//// userDetail.setRoles(Arrays.asList("ROLE_USER")); +//// } +// return userDetail; +// } +} diff --git a/authorization-server2/src/main/resources/application.properties b/authorization-server2/src/main/resources/application.properties new file mode 100644 index 0000000..ad903d1 --- /dev/null +++ b/authorization-server2/src/main/resources/application.properties @@ -0,0 +1,26 @@ +server.port=8081 +server.servlet.context-path=/auth + +security.oauth2.authorization.check-token-access=isAuthenticated() + +spring.main.allow-bean-definition-overriding=true +spring.datasource.url=jdbc:mysql://127.0.0.1/oauth2?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC +spring.datasource.username=root +spring.datasource.password=uneed3515 +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.database = MYSQL +spring.jpa.hibernate.ddl-auto=update +spring.jpa.generate-ddl=true +spring.jpa.show-sql=true +#spring.jpa.generate-ddl=false +#spring.jpa.hibernate.ddl-auto=none + + + +logging.level.web=debug +spring.http.log-request-details=true \ No newline at end of file diff --git a/authorization-server2/src/test/java/io/bluemoon/authorizationserver2/AuthorizationServer2ApplicationTests.java b/authorization-server2/src/test/java/io/bluemoon/authorizationserver2/AuthorizationServer2ApplicationTests.java new file mode 100644 index 0000000..89f2494 --- /dev/null +++ b/authorization-server2/src/test/java/io/bluemoon/authorizationserver2/AuthorizationServer2ApplicationTests.java @@ -0,0 +1,16 @@ +package io.bluemoon.authorizationserver2; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class AuthorizationServer2ApplicationTests { + + @Test + public void contextLoads() { + } + +}