├── .gitignore ├── LICENSE.txt ├── README.md ├── oauth-client ├── oauth-java-client │ ├── .gitignore │ ├── .swagger-codegen-ignore │ ├── .swagger-codegen │ │ └── VERSION │ ├── .travis.yml │ ├── README.md │ ├── build.gradle │ ├── build.sbt │ ├── docs │ │ ├── AuthCodeVO.md │ │ ├── ClientVO.md │ │ ├── OAuth2AccessToken.md │ │ ├── OAuth2RefreshToken.md │ │ ├── Oauth2EndpointApi.md │ │ └── ValidityVO.md │ ├── git_push.sh │ ├── gradle.properties │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── pom.xml │ ├── settings.gradle │ ├── src │ │ ├── main │ │ │ ├── AndroidManifest.xml │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── pphh │ │ │ │ └── oauth │ │ │ │ └── client │ │ │ │ ├── ApiCallback.java │ │ │ │ ├── ApiClient.java │ │ │ │ ├── ApiException.java │ │ │ │ ├── ApiResponse.java │ │ │ │ ├── Configuration.java │ │ │ │ ├── GzipRequestInterceptor.java │ │ │ │ ├── JSON.java │ │ │ │ ├── Pair.java │ │ │ │ ├── ProgressRequestBody.java │ │ │ │ ├── ProgressResponseBody.java │ │ │ │ ├── StringUtil.java │ │ │ │ ├── api │ │ │ │ └── OAuth2EndpointApi.java │ │ │ │ ├── auth │ │ │ │ ├── ApiKeyAuth.java │ │ │ │ ├── Authentication.java │ │ │ │ ├── HttpBasicAuth.java │ │ │ │ ├── OAuth.java │ │ │ │ └── OAuthFlow.java │ │ │ │ └── model │ │ │ │ ├── AuthCodeVO.java │ │ │ │ ├── ClientVO.java │ │ │ │ ├── OAuth2AccessToken.java │ │ │ │ └── ValidityVO.java │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── pphh │ │ │ └── oauth │ │ │ └── client │ │ │ └── api │ │ │ └── Oauth2EndpointApiTest.java │ └── swagger-api │ │ └── swagger.yaml ├── oauth-spring-boot-autoconfigure │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── pphh │ │ │ └── oauth │ │ │ └── config │ │ │ ├── ClientAutoConfiguration.java │ │ │ └── FilterAutoConfiguration.java │ │ └── resources │ │ └── META-INF │ │ └── spring.factories ├── oauth-spring-boot-websupport │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── com │ │ └── pphh │ │ └── oauth │ │ └── client │ │ └── webcontroller │ │ └── OAuthClientController.java ├── oauth-spring-web-filter │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── com │ │ └── pphh │ │ └── oauth │ │ └── client │ │ ├── constant │ │ ├── OAuthCheckType.java │ │ └── TokenStoreType.java │ │ ├── filter │ │ ├── OAuthSpringFilter.java │ │ └── UserInfoFilter.java │ │ └── utils │ │ └── CookieUtil.java └── pom.xml ├── oauth-core ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── pphh │ └── oauth │ └── core │ ├── constant │ ├── AuthorizeResponseType.java │ └── GrantType.java │ ├── identity │ ├── DefaultIdentity.java │ └── Identity.java │ ├── idp │ ├── Authentication.java │ ├── AuthenticationProvider.java │ ├── IdentityProvider.java │ └── IdentityProviderConfiguration.java │ ├── response │ ├── MessageType.java │ └── Response.java │ └── utils │ ├── BasicAuthUtil.java │ └── JwtUtil.java ├── oauth-front ├── .babelrc ├── .postcssrc.js ├── README.md ├── package.json ├── src │ ├── api │ │ ├── index.js │ │ ├── restApi.js │ │ └── service │ │ │ ├── accountService.js │ │ │ ├── index.js │ │ │ └── oauthService.js │ ├── assets │ │ ├── common.css │ │ └── img │ │ │ └── dog.jpg │ ├── components │ │ ├── Footer.vue │ │ ├── Header.vue │ │ └── SiderBar.vue │ ├── index.html │ ├── main.js │ ├── pages │ │ ├── Blank.vue │ │ ├── Layout.vue │ │ ├── LayoutAuth.vue │ │ ├── Login.vue │ │ ├── admin │ │ │ ├── ApprovedSiteList.vue │ │ │ ├── AuditLogList.vue │ │ │ ├── ClientList.vue │ │ │ ├── Index.vue │ │ │ ├── TokenList.vue │ │ │ └── UserList.vue │ │ ├── dev │ │ │ ├── Index.vue │ │ │ ├── MyClient.vue │ │ │ └── Register.vue │ │ ├── oauth2 │ │ │ └── Authorize.vue │ │ ├── user │ │ │ ├── ApprovedSite.vue │ │ │ ├── Index.vue │ │ │ └── MyToken.vue │ │ └── useraccount │ │ │ ├── ChangePwd.vue │ │ │ ├── Index.vue │ │ │ └── UserBasic.vue │ ├── router │ │ └── index.js │ ├── store │ │ ├── index.js │ │ ├── model │ │ │ ├── app.js │ │ │ └── oauth.js │ │ └── mutation-types.js │ ├── utils │ │ ├── dateUtil.js │ │ ├── encryptUtil.js │ │ ├── jwtTokenUtil.js │ │ └── lastVisitedUtil.js │ └── vendor.js └── webpack.config.js ├── oauth-server ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── pphh │ │ └── oauth │ │ ├── OAuthServer.java │ │ ├── aop │ │ ├── AuthorizationAspect.java │ │ ├── ExceptionAspect.java │ │ ├── ResourcePointCuts.java │ │ └── WebLogAspect.java │ │ ├── config │ │ ├── IdentityProviderConfig.java │ │ ├── LDAPConfiguration.java │ │ ├── SwaggerConfig.java │ │ └── UserAuditorAware.java │ │ ├── constant │ │ └── SecurityActionType.java │ │ ├── controller │ │ ├── AccountController.java │ │ ├── AuditController.java │ │ ├── ClientMgtController.java │ │ ├── DockerRegistryAuthEndpoint.java │ │ ├── OAuth2Endpoint.java │ │ ├── ScopeMgtController.java │ │ ├── SessionMgtController.java │ │ ├── TokenMgtController.java │ │ └── UserMgtController.java │ │ ├── dao │ │ ├── AccessTokenRepository.java │ │ ├── ApprovedScopeRepository.java │ │ ├── ApprovedSiteRepository.java │ │ ├── AuditLogRepository.java │ │ ├── AuthenticationHolderRepository.java │ │ ├── AuthorizationCodeRepository.java │ │ ├── BaseJpaRepository.java │ │ ├── ClientRepository.java │ │ ├── ClientScopeRepository.java │ │ ├── RefreshTokenRepository.java │ │ ├── SecurityActionRepository.java │ │ └── UserRepository.java │ │ ├── exception │ │ ├── BaseException.java │ │ └── UnAuthorizeException.java │ │ ├── filter │ │ └── JwtFilter.java │ │ ├── idp │ │ └── local │ │ │ ├── LocalAuthenticationProvider.java │ │ │ ├── LocalIdentityProvider.java │ │ │ └── LocalProviderConfiguration.java │ │ ├── manager │ │ ├── ClientManager.java │ │ ├── EntityMapper.java │ │ ├── TokenManager.java │ │ └── UserOwnedRrcManager.java │ │ ├── oauth2 │ │ ├── OAuth2AuthCodeServiceImpl.java │ │ └── OAuth2AuthTokenServiceImpl.java │ │ ├── po │ │ ├── AccessTokenEntity.java │ │ ├── ApprovedScopeEntity.java │ │ ├── ApprovedSiteEntity.java │ │ ├── AuditLogEntity.java │ │ ├── AuthenticationHolderEntity.java │ │ ├── AuthorizationCodeEntity.java │ │ ├── BaseEntity.java │ │ ├── ClientEntity.java │ │ ├── ClientScopeEntity.java │ │ ├── RefreshTokenEntity.java │ │ ├── UserEntity.java │ │ └── UserSecurityActionEntity.java │ │ ├── scheduler │ │ └── OAuth2TokenExpireUtil.java │ │ ├── service │ │ ├── ApprovedSiteService.java │ │ ├── AuthHolderService.java │ │ ├── ClientService.java │ │ ├── LdapService.java │ │ ├── MailService.java │ │ ├── MetricService.java │ │ ├── OAuth2Service.java │ │ ├── UserService.java │ │ └── impl │ │ │ ├── ApprovedSiteServiceImpl.java │ │ │ ├── AuditService.java │ │ │ ├── AuthHolderServiceImpl.java │ │ │ ├── ClientServiceImpl.java │ │ │ ├── LdapServiceImpl.java │ │ │ ├── MetricServiceImpl.java │ │ │ ├── OAuth2ServiceImpl.java │ │ │ ├── SmtpMailServiceImpl.java │ │ │ └── UserServiceImpl.java │ │ ├── utils │ │ ├── ConvertUtil.java │ │ ├── EnvProperty.java │ │ ├── LdapProperty.java │ │ ├── Md5Tool.java │ │ └── RequestContextUtil.java │ │ └── vo │ │ ├── AuthCodeVO.java │ │ ├── AuthTokenVO.java │ │ ├── ClientCheckResultVO.java │ │ ├── ClientVO.java │ │ ├── GrantRequestVO.java │ │ ├── PageVO.java │ │ ├── UserVO.java │ │ └── ValidityVO.java │ └── resources │ ├── application.properties │ └── init.sql ├── pom.xml └── sample ├── demo-front-jquery ├── README.md ├── app.js ├── package.json └── src │ ├── callback.html │ ├── index.html │ └── jwt-token.js ├── demo-front-vue-spring-boot-web ├── README.md ├── front-vue │ ├── .babelrc │ ├── .postcssrc.js │ ├── package.json │ ├── src │ │ ├── api │ │ │ ├── index.js │ │ │ ├── restApi.js │ │ │ └── service │ │ │ │ ├── authService.js │ │ │ │ └── index.js │ │ ├── assets │ │ │ ├── common.css │ │ │ └── img │ │ │ │ ├── dog.jpg │ │ │ │ └── fail_login.jpg │ │ ├── components │ │ │ ├── Footer.vue │ │ │ ├── Header.vue │ │ │ └── SiderBar.vue │ │ ├── index.html │ │ ├── main.js │ │ ├── pages │ │ │ ├── Blank.vue │ │ │ ├── Layout.vue │ │ │ └── Login.vue │ │ ├── router │ │ │ └── index.js │ │ ├── store │ │ │ ├── index.js │ │ │ └── model │ │ │ │ └── oauth.js │ │ ├── utils │ │ │ ├── jwtTokenUtil.js │ │ │ └── lastVisitUtil.js │ │ └── vendor.js │ └── webpack.config.js ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── pphh │ │ └── oauth │ │ └── sample │ │ ├── Application.java │ │ ├── AutoConfiguration.java │ │ └── TestController.java │ └── resources │ └── application.properties ├── demo-front-vue ├── .babelrc ├── .postcssrc.js ├── README.md ├── package.json ├── src │ ├── api │ │ ├── index.js │ │ ├── restApi.js │ │ └── service │ │ │ ├── authService.js │ │ │ └── index.js │ ├── assets │ │ ├── common.css │ │ └── img │ │ │ ├── dog.jpg │ │ │ └── fail_login.jpg │ ├── components │ │ ├── Footer.vue │ │ ├── Header.vue │ │ └── SiderBar.vue │ ├── index.html │ ├── main.js │ ├── pages │ │ ├── Blank.vue │ │ ├── Layout.vue │ │ └── Login.vue │ ├── router │ │ └── index.js │ ├── store │ │ ├── index.js │ │ └── model │ │ │ └── oauth.js │ ├── utils │ │ ├── jwtTokenUtil.js │ │ └── lastVisitUtil.js │ └── vendor.js └── webpack.config.js ├── demo-spring-boot-web ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── pphh │ │ └── oauth │ │ └── sample │ │ ├── App.java │ │ ├── AppProperties.java │ │ └── UserCredential.java │ └── resources │ ├── application.properties │ └── static │ ├── index.html │ ├── login.html │ └── logout.html ├── demo-web-service ├── README.md ├── pom.xml ├── resource-client │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── pphh │ │ │ └── oauth │ │ │ └── sample │ │ │ └── ResourceClient.java │ │ └── resources │ │ └── application.properties └── resource-server │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── com │ │ └── pphh │ │ └── oauth │ │ └── sample │ │ └── ResourceServer.java │ └── resources │ └── application.properties └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | # maven ignore 2 | target/ 3 | *.jar 4 | *.war 5 | *.zip 6 | *.tar 7 | *.tar.gz 8 | *pom.xml.versionsBackup 9 | 10 | # eclipse ignore 11 | .settings/ 12 | .project 13 | .classpath 14 | 15 | # idea ignore 16 | .idea/ 17 | *.ipr 18 | *.iml 19 | *.iws 20 | 21 | # temp ignore 22 | logs/ 23 | *.docpom.xml 24 | *.log 25 | *.cache 26 | *.diff 27 | *.patch 28 | *.tmp 29 | 30 | # system ignore 31 | .DS_Store 32 | Thumbs.db 33 | 34 | *.class 35 | 36 | # front project 37 | auth-front/dist/ 38 | auth-front/node_modules/ 39 | auth-front/.idea 40 | auth-front/*.iws 41 | auth-front/*.iml 42 | auth-front/*.ipr 43 | 44 | # backend server project 45 | auth-server/.idea 46 | auth-server/target/ 47 | auth-server/.settings/ 48 | auth-server/.project 49 | auth-server/.classpath 50 | auth-server/.mvn/ 51 | auth-server/mvnw 52 | auth-server/mvnw.cmd 53 | 54 | # node project 55 | node_modules/ -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # exclude jar for gradle wrapper 12 | !gradle/wrapper/*.jar 13 | 14 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 15 | hs_err_pid* 16 | 17 | # build files 18 | **/target 19 | target 20 | .gradle 21 | build 22 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/.swagger-codegen-ignore: -------------------------------------------------------------------------------- 1 | # Swagger Codegen Ignore 2 | # Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen 3 | 4 | # Use this file to prevent files from being overwritten by the generator. 5 | # The patterns follow closely to .gitignore or .dockerignore. 6 | 7 | # As an example, the C# client generator defines ApiClient.cs. 8 | # You can make changes and tell Swagger Codgen to ignore just this file by uncommenting the following line: 9 | #ApiClient.cs 10 | 11 | # You can match any string of characters against a directory, file or extension with a single asterisk (*): 12 | #foo/*/qux 13 | # The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux 14 | 15 | # You can recursively match patterns against a directory, file or extension with a double asterisk (**): 16 | #foo/**/qux 17 | # This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux 18 | 19 | # You can also negate patterns with an exclamation (!). 20 | # For example, you can ignore all files in a docs folder with the file extension .md: 21 | #docs/*.md 22 | # Then explicitly reverse the ignore rule for a single file: 23 | #!docs/README.md 24 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/.swagger-codegen/VERSION: -------------------------------------------------------------------------------- 1 | 2.3.1 -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/.travis.yml: -------------------------------------------------------------------------------- 1 | # 2 | # Generated by: https://github.com/swagger-api/swagger-codegen.git 3 | # 4 | language: java 5 | jdk: 6 | - oraclejdk8 7 | - oraclejdk7 8 | before_install: 9 | # ensure gradlew has proper permission 10 | - chmod a+x ./gradlew 11 | script: 12 | # test using maven 13 | - mvn test 14 | # uncomment below to test using gradle 15 | # - gradle test 16 | # uncomment below to test using sbt 17 | # - sbt test 18 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/build.sbt: -------------------------------------------------------------------------------- 1 | lazy val root = (project in file(".")). 2 | settings( 3 | organization := "io.swagger", 4 | name := "swagger-java-client", 5 | version := "1.0.0", 6 | scalaVersion := "2.11.4", 7 | scalacOptions ++= Seq("-feature"), 8 | javacOptions in compile ++= Seq("-Xlint:deprecation"), 9 | publishArtifact in (Compile, packageDoc) := false, 10 | resolvers += Resolver.mavenLocal, 11 | libraryDependencies ++= Seq( 12 | "io.swagger" % "swagger-annotations" % "1.5.15", 13 | "com.squareup.okhttp" % "okhttp" % "2.7.5", 14 | "com.squareup.okhttp" % "logging-interceptor" % "2.7.5", 15 | "com.google.code.gson" % "gson" % "2.8.1", 16 | "org.threeten" % "threetenbp" % "1.3.5" % "compile", 17 | "io.gsonfire" % "gson-fire" % "1.8.0" % "compile", 18 | "junit" % "junit" % "4.12" % "test", 19 | "com.novocode" % "junit-interface" % "0.10" % "test" 20 | ) 21 | ) 22 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/docs/AuthCodeVO.md: -------------------------------------------------------------------------------- 1 | 2 | # AuthCodeVO 3 | 4 | ## Properties 5 | Name | Type | Description | Notes 6 | ------------ | ------------- | ------------- | ------------- 7 | **clientName** | **String** | | [optional] 8 | **code** | **String** | | [optional] 9 | **expiration** | [**OffsetDateTime**](OffsetDateTime.md) | | [optional] 10 | **id** | **Long** | | [optional] 11 | **insertTime** | [**OffsetDateTime**](OffsetDateTime.md) | | [optional] 12 | **redirectUrl** | **String** | | [optional] 13 | **userName** | **String** | | [optional] 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/docs/ClientVO.md: -------------------------------------------------------------------------------- 1 | 2 | # ClientVO 3 | 4 | ## Properties 5 | Name | Type | Description | Notes 6 | ------------ | ------------- | ------------- | ------------- 7 | **basicAuth** | **String** | | [optional] 8 | **clientId** | **String** | | [optional] 9 | **clientSecret** | **String** | | [optional] 10 | **description** | **String** | | [optional] 11 | **id** | **Long** | | [optional] 12 | **ownerName** | **String** | | [optional] 13 | **redirectUrl** | **String** | | [optional] 14 | **rememberChoice** | **String** | | [optional] 15 | **respType** | **String** | | [optional] 16 | **scopes** | **List<String>** | | [optional] 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/docs/OAuth2AccessToken.md: -------------------------------------------------------------------------------- 1 | 2 | # OAuth2AccessToken 3 | 4 | ## Properties 5 | Name | Type | Description | Notes 6 | ------------ | ------------- | ------------- | ------------- 7 | **additionalInformation** | **Object** | | [optional] 8 | **expiration** | [**OffsetDateTime**](OffsetDateTime.md) | | [optional] 9 | **expired** | **Boolean** | | [optional] 10 | **expiresIn** | **Integer** | | [optional] 11 | **refreshToken** | [**OAuth2RefreshToken**](OAuth2RefreshToken.md) | | [optional] 12 | **scope** | **List<String>** | | [optional] 13 | **tokenType** | **String** | | [optional] 14 | **value** | **String** | | [optional] 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/docs/OAuth2RefreshToken.md: -------------------------------------------------------------------------------- 1 | 2 | # OAuth2RefreshToken 3 | 4 | ## Properties 5 | Name | Type | Description | Notes 6 | ------------ | ------------- | ------------- | ------------- 7 | **value** | **String** | | [optional] 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/docs/ValidityVO.md: -------------------------------------------------------------------------------- 1 | 2 | # ValidityVO 3 | 4 | ## Properties 5 | Name | Type | Description | Notes 6 | ------------ | ------------- | ------------- | ------------- 7 | **isValid** | **Boolean** | | [optional] 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/git_push.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ref: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/ 3 | # 4 | # Usage example: /bin/sh ./git_push.sh wing328 swagger-petstore-perl "minor update" 5 | 6 | git_user_id=$1 7 | git_repo_id=$2 8 | release_note=$3 9 | 10 | if [ "$git_user_id" = "" ]; then 11 | git_user_id="" 12 | echo "[INFO] No command line input provided. Set \$git_user_id to $git_user_id" 13 | fi 14 | 15 | if [ "$git_repo_id" = "" ]; then 16 | git_repo_id="" 17 | echo "[INFO] No command line input provided. Set \$git_repo_id to $git_repo_id" 18 | fi 19 | 20 | if [ "$release_note" = "" ]; then 21 | release_note="" 22 | echo "[INFO] No command line input provided. Set \$release_note to $release_note" 23 | fi 24 | 25 | # Initialize the local directory as a Git repository 26 | git init 27 | 28 | # Adds the files in the local repository and stages them for commit. 29 | git add . 30 | 31 | # Commits the tracked changes and prepares them to be pushed to a remote repository. 32 | git commit -m "$release_note" 33 | 34 | # Sets the new remote 35 | git_remote=`git remote` 36 | if [ "$git_remote" = "" ]; then # git remote not defined 37 | 38 | if [ "$GIT_TOKEN" = "" ]; then 39 | echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git credential in your environment." 40 | git remote add origin https://github.com/${git_user_id}/${git_repo_id}.git 41 | else 42 | git remote add origin https://${git_user_id}:${GIT_TOKEN}@github.com/${git_user_id}/${git_repo_id}.git 43 | fi 44 | 45 | fi 46 | 47 | git pull origin master 48 | 49 | # Pushes (Forces) the changes in the local repository up to the remote repository 50 | echo "Git pushing to https://github.com/${git_user_id}/${git_repo_id}.git" 51 | git push origin master 2>&1 | grep -v 'To https' 52 | 53 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/gradle.properties: -------------------------------------------------------------------------------- 1 | # Uncomment to build for Android 2 | #target = android -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peipeihh/simple-oauth2/5cbbde9cdfd9fd2e75d1b3f087ab7cde02abed66/oauth-client/oauth-java-client/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue May 17 23:08:05 CST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.6-bin.zip 7 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = "swagger-java-client" -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/src/main/java/com/pphh/oauth/client/ApiCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 统一认证和授权系统 3 | * 更多信息请联系pphh 4 | * 5 | * OpenAPI spec version: 1.0 6 | * 7 | * 8 | * NOTE: This class is auto generated by the swagger code generator program. 9 | * https://github.com/swagger-api/swagger-codegen.git 10 | * Do not edit the class manually. 11 | */ 12 | 13 | 14 | package com.pphh.oauth.client; 15 | 16 | import java.util.Map; 17 | import java.util.List; 18 | 19 | /** 20 | * Callback for asynchronous API call. 21 | * 22 | * @param The return type 23 | */ 24 | public interface ApiCallback { 25 | /** 26 | * This is called when the API call fails. 27 | * 28 | * @param e The exception causing the failure 29 | * @param statusCode Status code of the response if available, otherwise it would be 0 30 | * @param responseHeaders Headers of the response if available, otherwise it would be null 31 | */ 32 | void onFailure(ApiException e, int statusCode, Map> responseHeaders); 33 | 34 | /** 35 | * This is called when the API call succeeded. 36 | * 37 | * @param result The result deserialized from response 38 | * @param statusCode Status code of the response 39 | * @param responseHeaders Headers of the response 40 | */ 41 | void onSuccess(T result, int statusCode, Map> responseHeaders); 42 | 43 | /** 44 | * This is called when the API upload processing. 45 | * 46 | * @param bytesWritten bytes Written 47 | * @param contentLength content length of request body 48 | * @param done write end 49 | */ 50 | void onUploadProgress(long bytesWritten, long contentLength, boolean done); 51 | 52 | /** 53 | * This is called when the API downlond processing. 54 | * 55 | * @param bytesRead bytes Read 56 | * @param contentLength content lenngth of the response 57 | * @param done Read end 58 | */ 59 | void onDownloadProgress(long bytesRead, long contentLength, boolean done); 60 | } 61 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/src/main/java/com/pphh/oauth/client/ApiResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 统一认证和授权系统 3 | * 更多信息请联系pphh 4 | * 5 | * OpenAPI spec version: 1.0 6 | * 7 | * 8 | * NOTE: This class is auto generated by the swagger code generator program. 9 | * https://github.com/swagger-api/swagger-codegen.git 10 | * Do not edit the class manually. 11 | */ 12 | 13 | 14 | package com.pphh.oauth.client; 15 | 16 | import java.util.List; 17 | import java.util.Map; 18 | 19 | /** 20 | * API response returned by API call. 21 | * 22 | * @param The type of data that is deserialized from response body 23 | */ 24 | public class ApiResponse { 25 | final private int statusCode; 26 | final private Map> headers; 27 | final private T data; 28 | 29 | /** 30 | * @param statusCode The status code of HTTP response 31 | * @param headers The headers of HTTP response 32 | */ 33 | public ApiResponse(int statusCode, Map> headers) { 34 | this(statusCode, headers, null); 35 | } 36 | 37 | /** 38 | * @param statusCode The status code of HTTP response 39 | * @param headers The headers of HTTP response 40 | * @param data The object deserialized from response bod 41 | */ 42 | public ApiResponse(int statusCode, Map> headers, T data) { 43 | this.statusCode = statusCode; 44 | this.headers = headers; 45 | this.data = data; 46 | } 47 | 48 | public int getStatusCode() { 49 | return statusCode; 50 | } 51 | 52 | public Map> getHeaders() { 53 | return headers; 54 | } 55 | 56 | public T getData() { 57 | return data; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/src/main/java/com/pphh/oauth/client/Configuration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 统一认证和授权系统 3 | * 更多信息请联系pphh 4 | * 5 | * OpenAPI spec version: 1.0 6 | * 7 | * 8 | * NOTE: This class is auto generated by the swagger code generator program. 9 | * https://github.com/swagger-api/swagger-codegen.git 10 | * Do not edit the class manually. 11 | */ 12 | 13 | 14 | package com.pphh.oauth.client; 15 | 16 | @javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2018-07-25T13:38:10.202Z") 17 | public class Configuration { 18 | private static ApiClient defaultApiClient = new ApiClient(); 19 | 20 | /** 21 | * Get the default API client, which would be used when creating API 22 | * instances without providing an API client. 23 | * 24 | * @return Default API client 25 | */ 26 | public static ApiClient getDefaultApiClient() { 27 | return defaultApiClient; 28 | } 29 | 30 | /** 31 | * Set the default API client, which would be used when creating API 32 | * instances without providing an API client. 33 | * 34 | * @param apiClient API client 35 | */ 36 | public static void setDefaultApiClient(ApiClient apiClient) { 37 | defaultApiClient = apiClient; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/src/main/java/com/pphh/oauth/client/Pair.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 统一认证和授权系统 3 | * 更多信息请联系pphh 4 | * 5 | * OpenAPI spec version: 1.0 6 | * 7 | * 8 | * NOTE: This class is auto generated by the swagger code generator program. 9 | * https://github.com/swagger-api/swagger-codegen.git 10 | * Do not edit the class manually. 11 | */ 12 | 13 | 14 | package com.pphh.oauth.client; 15 | 16 | @javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2018-07-25T13:38:10.202Z") 17 | public class Pair { 18 | private String name = ""; 19 | private String value = ""; 20 | 21 | public Pair (String name, String value) { 22 | setName(name); 23 | setValue(value); 24 | } 25 | 26 | private void setName(String name) { 27 | if (!isValidString(name)) return; 28 | 29 | this.name = name; 30 | } 31 | 32 | private void setValue(String value) { 33 | if (!isValidString(value)) return; 34 | 35 | this.value = value; 36 | } 37 | 38 | public String getName() { 39 | return this.name; 40 | } 41 | 42 | public String getValue() { 43 | return this.value; 44 | } 45 | 46 | private boolean isValidString(String arg) { 47 | if (arg == null) return false; 48 | if (arg.trim().isEmpty()) return false; 49 | 50 | return true; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/src/main/java/com/pphh/oauth/client/StringUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 统一认证和授权系统 3 | * 更多信息请联系pphh 4 | * 5 | * OpenAPI spec version: 1.0 6 | * 7 | * 8 | * NOTE: This class is auto generated by the swagger code generator program. 9 | * https://github.com/swagger-api/swagger-codegen.git 10 | * Do not edit the class manually. 11 | */ 12 | 13 | 14 | package com.pphh.oauth.client; 15 | 16 | @javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2018-07-25T13:38:10.202Z") 17 | public class StringUtil { 18 | /** 19 | * Check if the given array contains the given value (with case-insensitive comparison). 20 | * 21 | * @param array The array 22 | * @param value The value to search 23 | * @return true if the array contains the value 24 | */ 25 | public static boolean containsIgnoreCase(String[] array, String value) { 26 | for (String str : array) { 27 | if (value == null && str == null) return true; 28 | if (value != null && value.equalsIgnoreCase(str)) return true; 29 | } 30 | return false; 31 | } 32 | 33 | /** 34 | * Join an array of strings with the given separator. 35 | *

36 | * Note: This might be replaced by utility method from commons-lang or guava someday 37 | * if one of those libraries is added as dependency. 38 | *

39 | * 40 | * @param array The array of strings 41 | * @param separator The separator 42 | * @return the resulting string 43 | */ 44 | public static String join(String[] array, String separator) { 45 | int len = array.length; 46 | if (len == 0) return ""; 47 | 48 | StringBuilder out = new StringBuilder(); 49 | out.append(array[0]); 50 | for (int i = 1; i < len; i++) { 51 | out.append(separator).append(array[i]); 52 | } 53 | return out.toString(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/src/main/java/com/pphh/oauth/client/auth/ApiKeyAuth.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 统一认证和授权系统 3 | * 更多信息请联系pphh 4 | * 5 | * OpenAPI spec version: 1.0 6 | * 7 | * 8 | * NOTE: This class is auto generated by the swagger code generator program. 9 | * https://github.com/swagger-api/swagger-codegen.git 10 | * Do not edit the class manually. 11 | */ 12 | 13 | 14 | package com.pphh.oauth.client.auth; 15 | 16 | import com.pphh.oauth.client.Pair; 17 | 18 | import java.util.Map; 19 | import java.util.List; 20 | 21 | @javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2018-07-25T13:38:10.202Z") 22 | public class ApiKeyAuth implements Authentication { 23 | private final String location; 24 | private final String paramName; 25 | 26 | private String apiKey; 27 | private String apiKeyPrefix; 28 | 29 | public ApiKeyAuth(String location, String paramName) { 30 | this.location = location; 31 | this.paramName = paramName; 32 | } 33 | 34 | public String getLocation() { 35 | return location; 36 | } 37 | 38 | public String getParamName() { 39 | return paramName; 40 | } 41 | 42 | public String getApiKey() { 43 | return apiKey; 44 | } 45 | 46 | public void setApiKey(String apiKey) { 47 | this.apiKey = apiKey; 48 | } 49 | 50 | public String getApiKeyPrefix() { 51 | return apiKeyPrefix; 52 | } 53 | 54 | public void setApiKeyPrefix(String apiKeyPrefix) { 55 | this.apiKeyPrefix = apiKeyPrefix; 56 | } 57 | 58 | @Override 59 | public void applyToParams(List queryParams, Map headerParams) { 60 | if (apiKey == null) { 61 | return; 62 | } 63 | String value; 64 | if (apiKeyPrefix != null) { 65 | value = apiKeyPrefix + " " + apiKey; 66 | } else { 67 | value = apiKey; 68 | } 69 | if ("query".equals(location)) { 70 | queryParams.add(new Pair(paramName, value)); 71 | } else if ("header".equals(location)) { 72 | headerParams.put(paramName, value); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/src/main/java/com/pphh/oauth/client/auth/Authentication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 统一认证和授权系统 3 | * 更多信息请联系pphh 4 | * 5 | * OpenAPI spec version: 1.0 6 | * 7 | * 8 | * NOTE: This class is auto generated by the swagger code generator program. 9 | * https://github.com/swagger-api/swagger-codegen.git 10 | * Do not edit the class manually. 11 | */ 12 | 13 | 14 | package com.pphh.oauth.client.auth; 15 | 16 | import com.pphh.oauth.client.Pair; 17 | 18 | import java.util.Map; 19 | import java.util.List; 20 | 21 | public interface Authentication { 22 | /** 23 | * Apply authentication settings to header and query params. 24 | * 25 | * @param queryParams List of query parameters 26 | * @param headerParams Map of header parameters 27 | */ 28 | void applyToParams(List queryParams, Map headerParams); 29 | } 30 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/src/main/java/com/pphh/oauth/client/auth/HttpBasicAuth.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 统一认证和授权系统 3 | * 更多信息请联系pphh 4 | * 5 | * OpenAPI spec version: 1.0 6 | * 7 | * 8 | * NOTE: This class is auto generated by the swagger code generator program. 9 | * https://github.com/swagger-api/swagger-codegen.git 10 | * Do not edit the class manually. 11 | */ 12 | 13 | 14 | package com.pphh.oauth.client.auth; 15 | 16 | import com.pphh.oauth.client.Pair; 17 | 18 | import com.squareup.okhttp.Credentials; 19 | 20 | import java.util.Map; 21 | import java.util.List; 22 | 23 | public class HttpBasicAuth implements Authentication { 24 | private String username; 25 | private String password; 26 | 27 | public String getUsername() { 28 | return username; 29 | } 30 | 31 | public void setUsername(String username) { 32 | this.username = username; 33 | } 34 | 35 | public String getPassword() { 36 | return password; 37 | } 38 | 39 | public void setPassword(String password) { 40 | this.password = password; 41 | } 42 | 43 | @Override 44 | public void applyToParams(List queryParams, Map headerParams) { 45 | if (username == null && password == null) { 46 | return; 47 | } 48 | headerParams.put("Authorization", Credentials.basic( 49 | username == null ? "" : username, 50 | password == null ? "" : password)); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/src/main/java/com/pphh/oauth/client/auth/OAuth.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 统一认证和授权系统 3 | * 更多信息请联系pphh 4 | * 5 | * OpenAPI spec version: 1.0 6 | * 7 | * 8 | * NOTE: This class is auto generated by the swagger code generator program. 9 | * https://github.com/swagger-api/swagger-codegen.git 10 | * Do not edit the class manually. 11 | */ 12 | 13 | 14 | package com.pphh.oauth.client.auth; 15 | 16 | import com.pphh.oauth.client.Pair; 17 | 18 | import java.util.Map; 19 | import java.util.List; 20 | 21 | @javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2018-07-25T13:38:10.202Z") 22 | public class OAuth implements Authentication { 23 | private String accessToken; 24 | 25 | public String getAccessToken() { 26 | return accessToken; 27 | } 28 | 29 | public void setAccessToken(String accessToken) { 30 | this.accessToken = accessToken; 31 | } 32 | 33 | @Override 34 | public void applyToParams(List queryParams, Map headerParams) { 35 | if (accessToken != null) { 36 | headerParams.put("Authorization", "Bearer " + accessToken); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/src/main/java/com/pphh/oauth/client/auth/OAuthFlow.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 统一认证和授权系统 3 | * 更多信息请联系pphh 4 | * 5 | * OpenAPI spec version: 1.0 6 | * 7 | * 8 | * NOTE: This class is auto generated by the swagger code generator program. 9 | * https://github.com/swagger-api/swagger-codegen.git 10 | * Do not edit the class manually. 11 | */ 12 | 13 | 14 | package com.pphh.oauth.client.auth; 15 | 16 | public enum OAuthFlow { 17 | accessCode, implicit, password, application 18 | } 19 | -------------------------------------------------------------------------------- /oauth-client/oauth-java-client/src/main/java/com/pphh/oauth/client/model/ValidityVO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 统一认证和授权系统 3 | * 更多信息请联系pphh 4 | * 5 | * OpenAPI spec version: 1.0 6 | * 7 | * 8 | * NOTE: This class is auto generated by the swagger code generator program. 9 | * https://github.com/swagger-api/swagger-codegen.git 10 | * Do not edit the class manually. 11 | */ 12 | 13 | 14 | package com.pphh.oauth.client.model; 15 | 16 | import java.util.Objects; 17 | 18 | import com.google.gson.annotations.SerializedName; 19 | import io.swagger.annotations.ApiModelProperty; 20 | 21 | /** 22 | * ValidityVO 23 | */ 24 | @javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2018-07-25T13:38:10.202Z") 25 | public class ValidityVO { 26 | @SerializedName("isValid") 27 | private Boolean isValid = null; 28 | 29 | public ValidityVO isValid(Boolean isValid) { 30 | this.isValid = isValid; 31 | return this; 32 | } 33 | 34 | /** 35 | * Get isValid 36 | * @return isValid 37 | **/ 38 | @ApiModelProperty(value = "") 39 | public Boolean isIsValid() { 40 | return isValid; 41 | } 42 | 43 | public void setIsValid(Boolean isValid) { 44 | this.isValid = isValid; 45 | } 46 | 47 | 48 | @Override 49 | public boolean equals(java.lang.Object o) { 50 | if (this == o) { 51 | return true; 52 | } 53 | if (o == null || getClass() != o.getClass()) { 54 | return false; 55 | } 56 | ValidityVO validityVO = (ValidityVO) o; 57 | return Objects.equals(this.isValid, validityVO.isValid); 58 | } 59 | 60 | @Override 61 | public int hashCode() { 62 | return Objects.hash(isValid); 63 | } 64 | 65 | 66 | @Override 67 | public String toString() { 68 | StringBuilder sb = new StringBuilder(); 69 | sb.append("class ValidityVO {\n"); 70 | 71 | sb.append(" isValid: ").append(toIndentedString(isValid)).append("\n"); 72 | sb.append("}"); 73 | return sb.toString(); 74 | } 75 | 76 | /** 77 | * Convert the given object to string with each line indented by 4 spaces 78 | * (except the first line). 79 | */ 80 | private String toIndentedString(java.lang.Object o) { 81 | if (o == null) { 82 | return "null"; 83 | } 84 | return o.toString().replace("\n", "\n "); 85 | } 86 | 87 | } 88 | 89 | -------------------------------------------------------------------------------- /oauth-client/oauth-spring-boot-autoconfigure/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## 1. 简介 4 | 本项目实现oauth client和oauth filter在spring boot项目中的自动化配置,方便用户的使用。 5 | 6 | ## 2. 引入依赖 7 | 对于maven项目,在pom.xml项目中添加了如下依赖类库, 8 | ``` 9 | 10 | com.pphh.demo 11 | oauth-spring-boot-autoconfigure 12 | 1.0-SNAPSHOT 13 | 14 | ``` 15 | 16 | ## 3. 使用说明 17 | 18 | ### 3.1 自动化配置oauth client 19 | 在项目的配置文件(src/main/resources/application.properties)中,添加如下配置项, 20 | ``` 21 | oauth.server.url = http://localhost 22 | ``` 23 | 24 | 然后在spring boot项目中,可以通过自动装配的方式获取oauth client实例。 25 | ``` 26 | @Autowired 27 | private OAuth2EndpointApi oAuthApi; 28 | ``` 29 | 30 | 接下来就可以使用oAuthApi来访问授权服务API。 31 | 32 | ### 3.2 自动化配置oauth filter 33 | 34 | 在项目的配置文件(src/main/resources/application.properties)中,添加如下配置项, 35 | ``` 36 | oauth.spring.filter.type = all-check-by-skip 37 | oauth.spring.filter.token.store.type = header 38 | oauth.spring.filter.token.name = oauth-token 39 | oauth.spring.filter.special.urls = 40 | oauth.spring.filter.audit.userinfo = audit-userinfo 41 | ``` 42 | 43 | 各个配置含义和使用说明如下, 44 | - oauth.spring.filter.type 必配项,过滤器类型,有all-skip-by-check和all-check-by-skip两种类型,配合特殊urls一起工作。 45 | * all-skip-by-check 跳过所有请求,但检查指定的特殊urls 46 | * all-check-by-skip 检查所有请求,但跳过特殊指定的urls 47 | - oauth.spring.filter.token.store.type 令牌在请求中的存储类型,有header和cookie两种类型,缺省为header 48 | - oauth.spring.filter.token.name 令牌在header/cookie的标识名,缺省为oauth-token 49 | - oauth.spring.filter.special.urls 特殊urls,多个URL可以通过逗号分开 50 | 51 | 特殊urls的使用样例, 52 | - .* 所有请求 53 | - /test.* 以test开头命名的所有请求 54 | - GET\&.* 所有GET请求 55 | - GET\&.\*,PUT\&.\* 所有GET和PUT请求 56 | 57 | 上述配置将对不同场景下oauth filter使用需求。其中,若下面两个配置项为空的话,则过滤器不会初始化。 58 | - oauth.spring.filter.type 过滤器类型 59 | - oauth.server.url 远程授权服务地址 60 | 61 | 过滤器检查请求中的令牌,将有如下行为, 62 | - 若令牌为空,否则返回400(BAD_REQUEST)的错误响应 63 | - 若令牌不合法或者已失效,则返回401(UNAUTHORIZED)的错误响应 64 | - 若令牌合法有效,则对请求放行 65 | 66 | 67 | ### 3.3 如何使用 68 | 请参考各个使用样例(sample目录下),更深入了解使用方法。 69 | -------------------------------------------------------------------------------- /oauth-client/oauth-spring-boot-autoconfigure/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | oauth-client 7 | com.pphh.demo 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | oauth-spring-boot-autoconfigure 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter 18 | 19 | 20 | com.pphh.demo 21 | oauth-core 22 | 23 | 24 | com.pphh.demo 25 | oauth-java-client 26 | 1.0.0 27 | 28 | 29 | com.pphh.demo 30 | oauth-spring-web-filter 31 | 1.0-SNAPSHOT 32 | 33 | 34 | -------------------------------------------------------------------------------- /oauth-client/oauth-spring-boot-autoconfigure/src/main/java/com/pphh/oauth/config/ClientAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.pphh.oauth.config; 2 | 3 | import com.pphh.oauth.client.api.OAuth2EndpointApi; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 6 | import org.springframework.context.annotation.Bean; 7 | 8 | /** 9 | * 自动化配置oauth api client 10 | * 11 | * @author huangyinhuang 12 | * @date 7/25/2018 13 | */ 14 | public class ClientAutoConfiguration { 15 | 16 | @Value("${oauth.server.url:http://localhost:8090}") 17 | private String authServerUrl; 18 | 19 | @Bean 20 | @ConditionalOnProperty(name = "oauth.server.url") 21 | public OAuth2EndpointApi oAuth2EndpointApi() { 22 | OAuth2EndpointApi oAuth2EndpointApi = new OAuth2EndpointApi(); 23 | oAuth2EndpointApi.getApiClient().setBasePath(authServerUrl); 24 | return oAuth2EndpointApi; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /oauth-client/oauth-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 2 | com.pphh.oauth.config.ClientAutoConfiguration,\ 3 | com.pphh.oauth.config.FilterAutoConfiguration -------------------------------------------------------------------------------- /oauth-client/oauth-spring-boot-websupport/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## 1. 简介 4 | 对于OAuth接入方,后端服务需要提供获取、刷新、吊销token的API接口,这是一个共同需求,本项目为spring boot项目提供后端的接口实现,方便用户的使用。 5 | 6 | ## 2. 引入依赖 7 | 对于maven项目,在pom.xml项目中添加了如下依赖类库, 8 | ``` 9 | 10 | com.pphh.demo 11 | oauth-spring-boot-websupport 12 | 1.0-SNAPSHOT 13 | 14 | ``` 15 | 16 | ## 3. 使用说明 17 | 18 | ### 3.1 自动化配置oauth client 19 | 在项目的配置文件(src/main/resources/application.properties)中,添加如下配置项, 20 | ``` 21 | oauth.server.url = http://localhost 22 | ``` 23 | url指向授权服务API地址。 24 | 25 | ### 3.2 自动化配置client注册信息 26 | 27 | 应用首先要到OAuth授权服务器注册,注册后将获取到如下信息, 28 | - client id 应用ID 29 | - client authorization 应用授权 30 | - call back 应用回调地址 31 | 32 | 在项目的配置文件(src/main/resources/application.properties)中,添加如下配置项, 33 | ``` 34 | oauth.client.callback = http://localhost:8888/#/login 35 | oauth.client.id = demo 36 | oauth.client.authorization = Basic ZGVtbzo1MGROOTI= 37 | ``` 38 | 上述是应用获取、刷新、吊销token所需要的配置信息。 39 | 40 | ### 3.3 如何使用 41 | 请参考使用样例sample/demo-front-vue-spring-boot-web,更深入了解使用方法。 42 | -------------------------------------------------------------------------------- /oauth-client/oauth-spring-boot-websupport/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | oauth-client 7 | com.pphh.demo 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | oauth-spring-boot-websupport 13 | 14 | 15 | 16 | com.pphh.demo 17 | oauth-spring-boot-autoconfigure 18 | 1.0-SNAPSHOT 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /oauth-client/oauth-spring-web-filter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | oauth-client 7 | com.pphh.demo 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | oauth-spring-web-filter 13 | 14 | 15 | 16 | com.pphh.demo 17 | oauth-java-client 18 | 1.0.0 19 | 20 | 21 | com.pphh.demo 22 | oauth-core 23 | 24 | 25 | org.springframework 26 | spring-web 27 | 4.3.12.RELEASE 28 | compile 29 | 30 | 31 | org.apache.tomcat.embed 32 | tomcat-embed-core 33 | 8.5.34 34 | compile 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /oauth-client/oauth-spring-web-filter/src/main/java/com/pphh/oauth/client/constant/OAuthCheckType.java: -------------------------------------------------------------------------------- 1 | package com.pphh.oauth.client.constant; 2 | 3 | /** 4 | * 过滤的检查类型 5 | * 6 | * @author huangyinhuang 7 | * @date 4/2/2018 8 | */ 9 | public enum OAuthCheckType { 10 | 11 | /** 12 | * 检查所有请求,跳过特定的URL 13 | */ 14 | ALL_CHECK_BY_SKIP, 15 | 16 | /** 17 | * 跳过所有请求,检查特定的URL 18 | */ 19 | ALL_SKIP_BY_CHECK, 20 | 21 | } 22 | -------------------------------------------------------------------------------- /oauth-client/oauth-spring-web-filter/src/main/java/com/pphh/oauth/client/constant/TokenStoreType.java: -------------------------------------------------------------------------------- 1 | package com.pphh.oauth.client.constant; 2 | 3 | /** 4 | * token的存储类型 5 | * 6 | * @author huangyinhuang 7 | * @date 3/26/2018 8 | */ 9 | public enum TokenStoreType { 10 | 11 | /** 12 | * 通过cookie存储token 13 | */ 14 | COOKIE, 15 | 16 | /** 17 | * 通过header存储token 18 | */ 19 | HEADER 20 | 21 | } 22 | -------------------------------------------------------------------------------- /oauth-client/oauth-spring-web-filter/src/main/java/com/pphh/oauth/client/filter/UserInfoFilter.java: -------------------------------------------------------------------------------- 1 | package com.pphh.oauth.client.filter; 2 | 3 | import com.pphh.oauth.client.constant.TokenStoreType; 4 | import com.pphh.oauth.client.utils.CookieUtil; 5 | import com.pphh.oauth.core.identity.Identity; 6 | import com.pphh.oauth.core.utils.JwtUtil; 7 | import org.springframework.web.filter.OncePerRequestFilter; 8 | 9 | import javax.servlet.FilterChain; 10 | import javax.servlet.ServletException; 11 | import javax.servlet.http.Cookie; 12 | import javax.servlet.http.HttpServletRequest; 13 | import javax.servlet.http.HttpServletResponse; 14 | import java.io.IOException; 15 | 16 | /** 17 | * 从请求中的token中解析用户信息,存储到http request attribute中 18 | * 19 | * @author huangyinhuang 20 | * @date 7/25/2018 21 | */ 22 | public class UserInfoFilter extends OncePerRequestFilter { 23 | 24 | private TokenStoreType tokenStoreType; 25 | private String tokenName; 26 | private String auditUserInfo; 27 | 28 | public UserInfoFilter(TokenStoreType tokenStoreType, String tokenName, String auditUserInfo) { 29 | this.tokenStoreType = tokenStoreType; 30 | this.tokenName = tokenName; 31 | this.auditUserInfo = auditUserInfo; 32 | } 33 | 34 | @Override 35 | protected void doFilterInternal(HttpServletRequest httpRequest, HttpServletResponse httpResponse, FilterChain filterChain) throws ServletException, IOException { 36 | String tokenValue = null; 37 | if (this.tokenStoreType == TokenStoreType.COOKIE) { 38 | Cookie cookie = CookieUtil.getCookieByName(httpRequest, this.tokenName); 39 | tokenValue = cookie != null ? cookie.getName() : null; 40 | } else if (this.tokenStoreType == TokenStoreType.HEADER) { 41 | tokenValue = httpRequest.getHeader(this.tokenName); 42 | } 43 | 44 | if (tokenValue != null && !tokenValue.equals("null")) { 45 | Identity identity = JwtUtil.decode(tokenValue); 46 | if (identity != null) { 47 | httpRequest.setAttribute(this.auditUserInfo, identity); 48 | } 49 | } 50 | 51 | filterChain.doFilter(httpRequest, httpResponse); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /oauth-client/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | simple-oauth2 7 | com.pphh.demo 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | oauth-client 13 | pom 14 | 15 | oauth-java-client 16 | oauth-spring-web-filter 17 | oauth-spring-boot-autoconfigure 18 | oauth-spring-boot-websupport 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /oauth-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | simple-oauth2 7 | com.pphh.demo 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | oauth-core 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-logging 19 | 20 | 21 | com.auth0 22 | java-jwt 23 | ${auth0.java.jwt.version} 24 | 25 | 26 | -------------------------------------------------------------------------------- /oauth-core/src/main/java/com/pphh/oauth/core/constant/AuthorizeResponseType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | 16 | package com.pphh.oauth.core.constant; 17 | 18 | /** 19 | * OAuth 2.0授权响应类别 20 | * 21 | * @author huangyinhuang 22 | * @date 7/2/2018 23 | */ 24 | public enum AuthorizeResponseType { 25 | 26 | 27 | /** 28 | * 授权码 29 | */ 30 | CODE, 31 | 32 | /** 33 | * 授权令牌 34 | */ 35 | TOKEN 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /oauth-core/src/main/java/com/pphh/oauth/core/constant/GrantType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.core.constant; 16 | 17 | /** 18 | * OAuth 2.0授权模式 19 | * 20 | * @author huangyinhuang 21 | * @date 7/2/2018 22 | */ 23 | public enum GrantType { 24 | 25 | /** 26 | * OAuth2授权码模式 27 | */ 28 | AUTHORIZATION_CODE, 29 | 30 | /** 31 | * OAuth2刷新码 32 | */ 33 | REFRESH_TOKEN, 34 | 35 | /** 36 | * OAuth2 Client Credential模式 37 | */ 38 | CLIENT_CREDENTIALS, 39 | 40 | /** 41 | * OAuth2 Resource Owner Password模式 42 | */ 43 | PASSWORD, 44 | 45 | /** 46 | * OAuth2简化模式 47 | */ 48 | IMPLICIT, 49 | 50 | } 51 | -------------------------------------------------------------------------------- /oauth-core/src/main/java/com/pphh/oauth/core/idp/Authentication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.core.idp; 16 | 17 | /** 18 | * a authentication which stores the identity's security info 19 | * 20 | * @author huangyinhuang 21 | * @date 7/2/2018 22 | */ 23 | public interface Authentication { 24 | 25 | /** 26 | * identity's credential 27 | * 28 | * @return credential 29 | */ 30 | Object getCredentials(); 31 | 32 | /** 33 | * identity's principal info 34 | * 35 | * @return principal 36 | */ 37 | Object getPrincipal(); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /oauth-core/src/main/java/com/pphh/oauth/core/idp/AuthenticationProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.core.idp; 16 | 17 | import com.pphh.oauth.core.identity.Identity; 18 | 19 | /** 20 | * The authentication provider, which helps to load identity by id/authentication 21 | * 22 | * @author huangyinhuang 23 | * @date 7/2/2018 24 | */ 25 | public interface AuthenticationProvider { 26 | 27 | /** 28 | * load identity by its id 29 | * 30 | * @param id identity id 31 | * @return identity object, return null if not found 32 | */ 33 | Identity load(String id); 34 | 35 | /** 36 | * load identity by its authentication 37 | * 38 | * @param authentication identity authentication 39 | * @return identity object, return null if not found 40 | */ 41 | Identity load(Authentication authentication); 42 | 43 | /** 44 | * the provider class supported 45 | * 46 | * @return supported class 47 | */ 48 | Class support(); 49 | 50 | } 51 | -------------------------------------------------------------------------------- /oauth-core/src/main/java/com/pphh/oauth/core/idp/IdentityProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.core.idp; 16 | 17 | /** 18 | * identity provider's support class 19 | * 20 | * @author huangyinhuang 21 | * @date 7/2/2018 22 | */ 23 | public interface IdentityProvider { 24 | 25 | /** 26 | * support class for identity provider configuration 27 | * 28 | * @return support class for identity provider configuration 29 | */ 30 | Class configuration(); 31 | 32 | /** 33 | * support class for authentication provider 34 | * 35 | * @return support class for authentication provider 36 | */ 37 | Class authenticationProvider(); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /oauth-core/src/main/java/com/pphh/oauth/core/idp/IdentityProviderConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.core.idp; 16 | 17 | /** 18 | * identity provider configuration 19 | * 20 | * @author huangyinhuang 21 | * @date 7/2/2018 22 | */ 23 | public interface IdentityProviderConfiguration { 24 | 25 | /** 26 | * support class of identity provider configuration 27 | * 28 | * @return support class of identity provider configuration 29 | */ 30 | Class support(); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /oauth-core/src/main/java/com/pphh/oauth/core/response/MessageType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.core.response; 16 | 17 | /** 18 | * common web service response, which provide uniform format of response code and message 19 | * 20 | * @author huangyinhuang 21 | * @date 7/2/2018 22 | */ 23 | public enum MessageType { 24 | 25 | 26 | /** 27 | * 请求成功消息 28 | */ 29 | SUCCESS(0, "请求成功完成。"), 30 | 31 | /** 32 | * 请求中发现错误 33 | */ 34 | ERROR(-1, "发现错误。"), 35 | 36 | /** 37 | * 请求中发现未知错误 38 | */ 39 | UNKNOWN(-4, "未知错误。"); 40 | 41 | private Integer code; 42 | private String msg; 43 | 44 | MessageType(Integer code, String msg) { 45 | this.code = code; 46 | this.msg = msg; 47 | } 48 | 49 | public Integer getCode() { 50 | return this.code; 51 | } 52 | 53 | public String getMsg() { 54 | return this.msg; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /oauth-core/src/main/java/com/pphh/oauth/core/utils/BasicAuthUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.core.utils; 16 | 17 | import java.nio.charset.Charset; 18 | import java.util.Base64; 19 | 20 | /** 21 | * Please add description here. 22 | * 23 | * @author huangyinhuang 24 | * @date 7/2/2018 25 | */ 26 | public class BasicAuthUtil { 27 | 28 | public static String BASIC_PREFIX = "Basic"; 29 | 30 | public static String[] decode(String authorization) { 31 | String[] values = null; 32 | 33 | if (authorization != null && authorization.startsWith(BASIC_PREFIX)) { 34 | // Authorization: Basic base64credentials 35 | String base64Credentials = authorization.substring(BASIC_PREFIX.length()).trim(); 36 | String credentials = new String(Base64.getDecoder().decode(base64Credentials), Charset.forName("UTF-8")); 37 | // credentials = username:password 38 | values = credentials.split(":", 2); 39 | } 40 | return values; 41 | 42 | } 43 | 44 | public static String encode(String id, String secret) { 45 | String str = String.format("%s:%s", id, secret); 46 | str = Base64.getEncoder().encodeToString(str.getBytes()); 47 | return String.format("%s %s", BASIC_PREFIX, str); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /oauth-front/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "es2015", 4 | "stage-2" 5 | ], 6 | "plugins": [ 7 | "transform-runtime", 8 | "transform-vue-jsx" 9 | ], 10 | "comments": true 11 | } 12 | -------------------------------------------------------------------------------- /oauth-front/.postcssrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | "plugins": { 3 | "autoprefixer": {} 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /oauth-front/README.md: -------------------------------------------------------------------------------- 1 | 2 | ## 1. 项目构建 3 | 本项目为前端node项目,技术栈为vue 2.0 + vue.router + vuex + bootstrap-vue/element + axios + element ui + echarts。 4 | 5 | ### 1.1 安装node并配置npm源 6 | 登录node官方网站安装node 3.10.8+。 7 | 8 | 配置npm源为淘宝源, 9 | - npm set registry "https://registry.npm.taobao.org/" 10 | 11 | 配置后可以通过npm config list查看。 12 | 13 | ### 1.2 前端项目构建 14 | 构建文件:./package.json 15 | 16 | 构建命令:npm install 17 | 18 | ## 2. 启动前端 19 | 命令:npm run dev 20 | 21 | 启动配置在./webpack.config.js文件中的devServer.host和devServer.port选项,默认启动在80端口。 22 | 23 | ## 3. 访问前端并登录 24 | 启动完毕后,在浏览器中打开如下访问地址, 25 | - http://localhost 26 | 然后使用初始账号admin/admin来登录。 27 | 28 | 注:登录功能需要后端服务启动,请见oauth-server的README文件。 29 | -------------------------------------------------------------------------------- /oauth-front/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "oauth-front", 3 | "version": "1.0.0", 4 | "description": "this is oauth front project.", 5 | "main": "webpack.config.js", 6 | "scripts": { 7 | "dev": "webpack-dev-server --inline --hot --env.dev", 8 | "build": "webpack -p --progress --hide-modules" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git@oauth-front.git" 13 | }, 14 | "keywords": [ 15 | "release", 16 | "paas" 17 | ], 18 | "author": "huangyinhuang", 19 | "license": "Apache License 2.0", 20 | "dependencies": { 21 | "vue": "^2.5.2", 22 | "vue-router": "^2.0.0", 23 | "vuex": "^2.3.1", 24 | "axios": "^0.16.2", 25 | "jwt-decode": "^2.1.0", 26 | "element-ui": "^2.0.9", 27 | "echarts": "^3.5.0" 28 | }, 29 | "devDependencies": { 30 | "babel": "^6.23.0", 31 | "babel-core": "^6.0.0", 32 | "babel-loader": "^6.0.0", 33 | "babel-plugin-component": "^0.9.1", 34 | "babel-preset-env": "^1.6.1", 35 | "babel-preset-es2015": "^6.24.1", 36 | "babel-preset-stage-2": "^6.24.1", 37 | "babel-preset-vue-app": "^1.2.0", 38 | "cross-env": "^1.0.6", 39 | "css-loader": "^0.23.1", 40 | "file-loader": "^0.8.5", 41 | "html-webpack-plugin": "^2.24.1", 42 | "jsonwebtoken": "^7.3.0", 43 | "postcss-loader": "^1.3.3", 44 | "request": "^2.79.0", 45 | "style-loader": "^0.13.1", 46 | "uglifyjs-webpack-plugin": "^0.4.6", 47 | "url-loader": "^0.5.8", 48 | "vue-loader": "^9.8.0", 49 | "vue-particles": "^1.0.9", 50 | "vue-style-loader": "^2.0.0", 51 | "webpack": "^2.4.5", 52 | "webpack-dev-server": "^2.4.5" 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /oauth-front/src/api/index.js: -------------------------------------------------------------------------------- 1 | import axios from 'axios' 2 | import service from './service' 3 | 4 | axios.defaults.timeout = 60000; 5 | axios.defaults.headers.common['Content-Type'] = 'application/json'; 6 | axios.defaults.headers.post['Content-Type'] = 'application/json'; 7 | axios.defaults.headers.put['Content-Type'] = 'application/json'; 8 | 9 | export const api = service; -------------------------------------------------------------------------------- /oauth-front/src/api/restApi.js: -------------------------------------------------------------------------------- 1 | import axios from 'axios' 2 | import jwtTokenUtil from '../utils/jwtTokenUtil' 3 | 4 | export default { 5 | 6 | doGetRequest(url){ 7 | let jwtToken = { 8 | "jwt-token": jwtTokenUtil.read() 9 | }; 10 | return axios.get(url, {headers: jwtToken}) 11 | .then((response) => Promise.resolve(response)) 12 | .catch((error) => Promise.reject(error)) 13 | }, 14 | doDeleteRequest(url){ 15 | let jwtToken = { 16 | "jwt-token": jwtTokenUtil.read() 17 | }; 18 | return axios.delete(url, {headers: jwtToken}) 19 | .then((response) => Promise.resolve(response)) 20 | .catch((error) => Promise.reject(error)) 21 | }, 22 | doPutRequest(url, data){ 23 | if (typeof(data) == "object") { 24 | data = JSON.stringify(data); 25 | } 26 | 27 | let jwtToken = { 28 | "jwt-token": jwtTokenUtil.read() 29 | }; 30 | return axios.put(url, data, {headers: jwtToken}) 31 | .then((response) => Promise.resolve(response)) 32 | .catch((error) => Promise.reject(error)) 33 | }, 34 | doPostRequest(url, data){ 35 | if (typeof(data) == "object") { 36 | data = JSON.stringify(data); 37 | } 38 | 39 | let jwtToken = { 40 | "jwt-token": jwtTokenUtil.read() 41 | }; 42 | return axios.post(url, data, {headers: jwtToken}) 43 | .then((response) => Promise.resolve(response)) 44 | .catch((error) => Promise.reject(error)) 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /oauth-front/src/api/service/accountService.js: -------------------------------------------------------------------------------- 1 | import restApi from '../restApi' 2 | 3 | export default { 4 | 5 | doLogin(request = {}){ 6 | let url = '/api/account/login'; 7 | return restApi.doPostRequest(url, request.token); 8 | }, 9 | registerAccount(request = {}){ 10 | let url = "api/account/register?email=" + request.usermail; 11 | return restApi.doPostRequest(url); 12 | }, 13 | fetchMyAccount(request = {}){ 14 | let url = "api/account/refreshPassword?email=" + request.usermail; 15 | return restApi.doPostRequest(url); 16 | }, 17 | saveAccountUserName(request = {}){ 18 | let url = "api/account/setUserName?email=" + request.usermail + "&username=" + request.username; 19 | return restApi.doPostRequest(url); 20 | }, 21 | saveAccountPassword(request = {}){ 22 | let url = "api/account/setPassword"; 23 | return restApi.doPostRequest(url, request.token); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /oauth-front/src/api/service/index.js: -------------------------------------------------------------------------------- 1 | import accountService from './accountService' 2 | import oauthService from './oauthService' 3 | 4 | export default { 5 | accountService, 6 | oauthService 7 | } 8 | -------------------------------------------------------------------------------- /oauth-front/src/assets/common.css: -------------------------------------------------------------------------------- 1 | html, body { 2 | height: 100%; 3 | } 4 | 5 | body { 6 | margin: 0px; 7 | } 8 | 9 | #app { 10 | height: 100%; 11 | } 12 | 13 | .header { 14 | background-color: rgb(32, 160, 255); 15 | position: relative; 16 | box-sizing: border-box; 17 | width: 100%; 18 | height: 70px; 19 | font-size: 22px; 20 | line-height: 70px; 21 | color: #fff; 22 | } 23 | 24 | .header .title { 25 | display: table; 26 | cursor: pointer; 27 | margin-left: 20px; 28 | float: left; 29 | font-size: 30px; 30 | } 31 | 32 | .header .title:hover { 33 | color: #337ab7; 34 | } 35 | 36 | .header .title i { 37 | font-size: 36px; 38 | } 39 | 40 | .header .logo { 41 | margin-right: 5px; 42 | } 43 | 44 | .nav-bar { 45 | padding-bottom: 20px; 46 | } 47 | 48 | .embeded_form { 49 | margin-top: 20px; 50 | padding: 10px 30px; 51 | border: 1px solid #eaeefb; 52 | border-radius: 4px; 53 | transition: .2s; 54 | } 55 | 56 | .query-input { 57 | width: 200px; 58 | margin-right: 40px; 59 | } 60 | 61 | .el-notification__content { 62 | margin: 6px 0; 63 | } 64 | -------------------------------------------------------------------------------- /oauth-front/src/assets/img/dog.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peipeihh/simple-oauth2/5cbbde9cdfd9fd2e75d1b3f087ab7cde02abed66/oauth-front/src/assets/img/dog.jpg -------------------------------------------------------------------------------- /oauth-front/src/components/Footer.vue: -------------------------------------------------------------------------------- 1 | 7 | 8 | 11 | 12 | -------------------------------------------------------------------------------- /oauth-front/src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | OAuth管理系统 7 | 8 | 9 | 10 | 11 |
12 | 13 |
14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /oauth-front/src/main.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by huangyinhuang on 11/20/2017. 3 | */ 4 | import Vue from 'vue' 5 | import Vuex from 'vuex' 6 | import VueRouter from 'vue-router' 7 | Vue.use(VueRouter); 8 | Vue.use(Vuex); 9 | import ElementUI from 'element-ui' 10 | import 'element-ui/lib/theme-chalk/index.css' 11 | Vue.use(ElementUI); 12 | 13 | import VueParticles from 'vue-particles' 14 | Vue.use(VueParticles); 15 | 16 | import axios from 'axios' 17 | import router from './router' 18 | import store from './store' 19 | import jwtTokenUtil from "./utils/jwtTokenUtil"; 20 | import jwt_decode from 'jwt-decode'; 21 | 22 | 23 | /** 24 | * enable axios ajax call in the vue component 25 | * please see the usage example in the ./pages/pages/demo/Ajax.vue 26 | * @type {AxiosStatic} 27 | */ 28 | Vue.prototype.$http = axios; 29 | 30 | /** 31 | * enable the development mode 32 | * @type {boolean} 33 | */ 34 | Vue.config.devtools = process.env.NODE_ENV === 'development'; 35 | 36 | /** 37 | * 为admin用户和local用户分配路由权限 38 | */ 39 | router.beforeEach ((to, from, next) => { 40 | // console.log(store.state.app.userRoles) 41 | let userRoles = jwtTokenUtil.getUserRoles(); 42 | if (to.name == 'admin' || to.name == 'clients' || to.name == 'users' || to.name == 'approvedsites' || to.name == 'tokens' || to.name == 'auditLogs') { 43 | let canGo = (userRoles != null) && userRoles.includes('admin'); 44 | if (canGo) { 45 | next(); 46 | } else { 47 | next({name: 'base'}); 48 | } 49 | } else if (to.name == 'useraccount' || to.name == 'basic' || to.name == 'changepwd') { 50 | let canGo = (userRoles != null) && userRoles.includes('local'); 51 | if (canGo) { 52 | next(); 53 | } else { 54 | next({name: 'base'}); 55 | } 56 | } else { 57 | next(); 58 | } 59 | }) 60 | 61 | /** 62 | * initialize the vue app with vuex store and vue router 63 | */ 64 | new Vue({ 65 | store, 66 | router, 67 | }).$mount('#app'); 68 | 69 | -------------------------------------------------------------------------------- /oauth-front/src/pages/Blank.vue: -------------------------------------------------------------------------------- 1 | 4 | 5 | 8 | 9 | -------------------------------------------------------------------------------- /oauth-front/src/pages/Layout.vue: -------------------------------------------------------------------------------- 1 | 15 | 16 | 45 | 46 | -------------------------------------------------------------------------------- /oauth-front/src/pages/LayoutAuth.vue: -------------------------------------------------------------------------------- 1 | 14 | 15 | 49 | 50 | -------------------------------------------------------------------------------- /oauth-front/src/pages/admin/Index.vue: -------------------------------------------------------------------------------- 1 | 6 | 7 | -------------------------------------------------------------------------------- /oauth-front/src/pages/dev/Index.vue: -------------------------------------------------------------------------------- 1 | 6 | 7 | -------------------------------------------------------------------------------- /oauth-front/src/pages/user/Index.vue: -------------------------------------------------------------------------------- 1 | 6 | 7 | -------------------------------------------------------------------------------- /oauth-front/src/pages/useraccount/Index.vue: -------------------------------------------------------------------------------- 1 | 6 | 7 | -------------------------------------------------------------------------------- /oauth-front/src/store/index.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import Vuex from 'vuex' 3 | import app from './model/app' 4 | import oauth from './model/oauth' 5 | Vue.use(Vuex); 6 | 7 | /** 8 | * detect current environment and set the debug configuration for vue store 9 | */ 10 | const debug_mode = process.env.NODE_ENV !== 'production'; 11 | 12 | /** 13 | * initialize the vuex store with actions/getters/modules 14 | */ 15 | export default new Vuex.Store({ 16 | modules: { 17 | app, 18 | oauth 19 | }, 20 | strict: debug_mode 21 | }) 22 | -------------------------------------------------------------------------------- /oauth-front/src/store/mutation-types.js: -------------------------------------------------------------------------------- 1 | /** 2 | * mutation variables which is used inside vuex store 3 | */ 4 | 5 | export const REFRESH_USER_INFO = 'REFRESH_USER_INFO'; 6 | export const REFRESH_EXPIRE_INFO = 'REFRESH_EXPIRE_INFO'; 7 | export const REFRESH_PROMPT_MESSAGE = 'REFRESH_PROMPT_MESSAGE'; 8 | export const SAVE_LAST_VISIT = 'SAVE_LAST_VISIT'; 9 | 10 | 11 | export const REFRESH_CLIENT_LIST = 'REFRESH_CLIENT_LIST'; 12 | export const REFRESH_CLIENTS_BY_PAGE = 'REFRESH_CLIENTS_BY_PAGE'; 13 | export const REFRESH_USER_LIST = 'REFRESH_USER_LIST'; 14 | export const REFRESH_USERS_BY_PAGE = 'REFRESH_USERS_BY_PAGE'; 15 | export const REFRESH_ACCESS_TOKEN_LIST = 'REFRESH_ACCESS_TOKEN_LIST'; 16 | export const REFRESH_ACCESS_TOKENS_BY_PAGE = 'REFRESH_ACCESS_TOKENS_BY_PAGE'; 17 | export const REFRESH_REFRESH_TOKEN_LIST = 'REFRESH_REFRESH_TOKEN_LIST'; 18 | export const REFRESH_REFRESH_TOKENS_BY_PAGE = 'REFRESH_REFRESH_TOKENS_BY_PAGE'; 19 | export const REFRESH_AUDIT_LOGS_BY_PAGE = 'REFRESH_AUDIT_LOGS_BY_PAGE'; 20 | 21 | export const REFRESH_MY_CLIENTS = "REFRESH_MY_CLIENTS"; 22 | export const REFRESH_MY_SESSIONS = "REFRESH_MY_SESSIONS"; 23 | export const REFRESH_SESSIONS_BY_PAGE = "REFRESH_SESSIONS_BY_PAGE"; 24 | export const REFRESH_AUTH_CODE_LIST = "REFRESH_AUTH_CODE_LIST"; 25 | export const REFRESH_AUTH_CODES_BY_PAGE = "REFRESH_AUTH_CODES_BY_PAGE"; 26 | export const REFRESH_MY_AUTH_CODES = "REFRESH_MY_AUTH_CODES"; 27 | export const REFRESH_MY_ACCESS_TOKENS = "REFRESH_MY_ACCESS_TOKENS"; 28 | export const REFRESH_MY_REFRESH_TOKENS = "REFRESH_MY_REFRESH_TOKENS"; 29 | 30 | export const REFRESH_CLIENT_VALID_STATUS = "REFRESH_CLIENT_VALID_STATUS"; 31 | export const REFRESH_AUTH_CODE = "REFRESH_AUTH_CODE"; 32 | -------------------------------------------------------------------------------- /oauth-front/src/utils/dateUtil.js: -------------------------------------------------------------------------------- 1 | const dateUtil = { 2 | 3 | formatDate(timestamp) { 4 | let sDate = ""; 5 | if (timestamp != null) { 6 | let date = new Date(timestamp); 7 | sDate = date.toLocaleString(); 8 | } 9 | return sDate; 10 | }, 11 | 12 | formatDateEx(timestamp, fmt) { 13 | let date = new Date(timestamp); 14 | 15 | if (/(y+)/.test(fmt)) { 16 | fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)); 17 | } 18 | 19 | let o = { 20 | 'M+': date.getMonth() + 1, 21 | 'd+': date.getDate(), 22 | 'h+': date.getHours(), 23 | 'm+': date.getMinutes(), 24 | 's+': date.getSeconds() 25 | }; 26 | 27 | for (let k in o) { 28 | if (new RegExp(`(${k})`).test(fmt)) { 29 | let str = o[k] + ''; 30 | 31 | if (fmt.replace(RegExp.$1, (RegExp.$1.length === 1))) { 32 | fmt = str; 33 | } else { 34 | fmt = ('00' + str).substr(str.length); 35 | } 36 | 37 | // fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : this.padLeftZero(str)); 38 | } 39 | } 40 | 41 | return fmt; 42 | }, 43 | 44 | padLeftZero(str) { 45 | return ('00' + str).substr(str.length); 46 | } 47 | 48 | }; 49 | 50 | export default dateUtil; 51 | 52 | -------------------------------------------------------------------------------- /oauth-front/src/utils/encryptUtil.js: -------------------------------------------------------------------------------- 1 | 2 | export default { 3 | 4 | generateToken(data){ 5 | let token = null; 6 | if (data != null) { 7 | data.timestamp = (new Date()).getTime(); 8 | let c = window.btoa(JSON.stringify(data)); 9 | token = c.split('').reverse().join('') + c.charAt(3) + c; 10 | } 11 | return token; 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /oauth-front/src/utils/jwtTokenUtil.js: -------------------------------------------------------------------------------- 1 | import jwt_decode from 'jwt-decode'; 2 | 3 | export default { 4 | 5 | save(token) { 6 | localStorage.setItem("jwt-token", token); 7 | }, 8 | 9 | read() { 10 | return localStorage.getItem("jwt-token"); 11 | }, 12 | 13 | clear() { 14 | localStorage.removeItem("jwt-token"); 15 | }, 16 | 17 | getUserRoles() { 18 | let userRoles = null; 19 | let jwt = localStorage.getItem("jwt-token"); 20 | if (jwt != null) { 21 | let jwtInfo = jwt_decode(jwt); 22 | if (jwtInfo != null) { 23 | userRoles = (jwtInfo.user_role != null) ? jwtInfo.user_role : null; 24 | if (userRoles != null) { 25 | userRoles = userRoles.split(','); 26 | } 27 | } 28 | } 29 | return userRoles; 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /oauth-front/src/utils/lastVisitedUtil.js: -------------------------------------------------------------------------------- 1 | 2 | export default { 3 | 4 | save(url){ 5 | localStorage.setItem("last-visited", url); 6 | }, 7 | 8 | read(){ 9 | return localStorage.getItem("last-visited"); 10 | }, 11 | 12 | clear(){ 13 | localStorage.removeItem("last-visited"); 14 | } 15 | 16 | } -------------------------------------------------------------------------------- /oauth-front/src/vendor.js: -------------------------------------------------------------------------------- 1 | /** 2 | * list all 3rd party modules here, which will be packaged as vendor.js 3 | */ 4 | 5 | import Vue from 'vue' 6 | import ElementUI from 'element-ui' 7 | -------------------------------------------------------------------------------- /oauth-server/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## 1. 项目构建 4 | 5 | 本项目为后端spring boot项目,技术栈为spring mvc + spring boot + spring hibernate jpa + mysql。 6 | 7 | 构建命令:mvn clean compile 8 | 9 | ## 2. 数据库配置 10 | 11 | 请执行数据库配置脚本, 12 | - src/main/resource/init.sql 13 | 14 | 该脚本将初始化所需的表,并添加如下信息, 15 | - 缺省账号:admin/admin 16 | - 演示所需的client:demo app( id = demo, authorization = Basic ZGVtbzo1MGROOTI=) 17 | 18 | ## 3. 启动应用 19 | 20 | 启动命令:java -jar oauth-server-1.0-SNAPSHOT.jar 21 | 22 | 若应用启动成功,在浏览器中打开如下访问地址, 23 | - http://localhost:8090/health 24 | 可以看到如下信息, 25 | ``` 26 | { 27 | "status": "UP", 28 | "diskSpace": { 29 | "status": "UP", 30 | "total": 314572795904, 31 | "free": 158274048000, 32 | "threshold": 10485760 33 | }, 34 | "db": { 35 | "status": "UP", 36 | "database": "MySQL", 37 | "hello": 1 38 | } 39 | } 40 | ``` 41 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/OAuthServer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth; 16 | 17 | import org.springframework.boot.SpringApplication; 18 | import org.springframework.boot.autoconfigure.SpringBootApplication; 19 | 20 | /** 21 | * OAuthServer 22 | * 23 | * @author huangyinhuang 24 | * @date 7/2/2018 25 | */ 26 | @SpringBootApplication 27 | public class OAuthServer { 28 | 29 | public static void main(String[] args) { 30 | SpringApplication.run(OAuthServer.class, args); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/aop/AuthorizationAspect.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.aop; 16 | 17 | import com.pphh.oauth.config.UserAuditorAware; 18 | import com.pphh.oauth.service.UserService; 19 | import com.pphh.oauth.utils.RequestContextUtil; 20 | import lombok.extern.slf4j.Slf4j; 21 | import org.aspectj.lang.JoinPoint; 22 | import org.aspectj.lang.annotation.Aspect; 23 | import org.aspectj.lang.annotation.Before; 24 | import org.springframework.beans.factory.annotation.Autowired; 25 | import org.springframework.core.annotation.Order; 26 | import org.springframework.stereotype.Component; 27 | 28 | /** 29 | * check user's information and its permission 30 | * 31 | * @author huangyinhuang 32 | * @date 7/2/2018 33 | */ 34 | @Aspect 35 | @Component 36 | @Slf4j 37 | @Order(12) 38 | public class AuthorizationAspect { 39 | 40 | @Autowired 41 | private UserService userService; 42 | 43 | @Before("ResourcePointCuts.apiController()") 44 | public void checkPermission(JoinPoint joinPoint) throws Throwable { 45 | // read user name from request attribute 46 | String userName = RequestContextUtil.getCurrentUserName(); 47 | 48 | if (userName != null && !userName.equals(UserAuditorAware.DEFAULT_SYSTEM_NAME)) { 49 | // if the user name 50 | // - doesn't exist in the database, add user into database 51 | // - exit in the database, update the visit time 52 | if (!userService.hasUser(userName)) { 53 | log.info("add user into database"); 54 | //authService.addUser(userName); 55 | } else { 56 | userService.updateLastVisitTime(userName); 57 | } 58 | } 59 | 60 | // TODO: check user's permission when user permission is ready 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/aop/ExceptionAspect.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.aop; 16 | 17 | import com.pphh.oauth.core.response.Response; 18 | import com.pphh.oauth.core.response.MessageType; 19 | import com.pphh.oauth.exception.BaseException; 20 | import lombok.extern.slf4j.Slf4j; 21 | import org.aspectj.lang.ProceedingJoinPoint; 22 | import org.aspectj.lang.annotation.Around; 23 | import org.aspectj.lang.annotation.Aspect; 24 | import org.springframework.core.annotation.Order; 25 | import org.springframework.stereotype.Component; 26 | 27 | import java.util.UUID; 28 | 29 | /** 30 | * Handle all the exceptions that are thrown from controller/service/dao 31 | * 32 | * @author huangyinhuang 33 | * @date 7/2/2018 34 | */ 35 | @Aspect 36 | @Component 37 | @Slf4j 38 | @Order(11) 39 | public class ExceptionAspect { 40 | 41 | @Around("ResourcePointCuts.webController()") 42 | public Object handleWebException(ProceedingJoinPoint apiMethod) { 43 | log.info("try to handle exception thrown from web controller method"); 44 | 45 | Object retVal = null; 46 | try { 47 | retVal = apiMethod.proceed(); 48 | } catch (BaseException e) { 49 | log.info(e.getMessage()); 50 | retVal = Response.mark(e.getMessageType(), e.getMessage()); 51 | } catch (Throwable throwable) { 52 | UUID uuid = UUID.randomUUID(); 53 | String msg = String.format("[%s] %s", uuid, throwable.getMessage()); 54 | log.error(msg, throwable); 55 | retVal = Response.mark(MessageType.UNKNOWN, "未知错误,请联系负责团队寻求更多帮助,定位GUID为[" + uuid + "]。"); 56 | } 57 | 58 | return retVal; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/aop/ResourcePointCuts.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.aop; 16 | 17 | import org.aspectj.lang.annotation.Pointcut; 18 | 19 | /** 20 | * Define the pointcuts for the spring aop handler 21 | * 22 | * @author huangyinhuang 23 | * @date 7/2/2018 24 | */ 25 | public class ResourcePointCuts { 26 | 27 | /** 28 | * all rest api, which includes web controller and oauth2 endpoint 29 | */ 30 | @Pointcut("execution(public * com.pphh.oauth.controller..*.*(..))") 31 | public void apiController() { 32 | } 33 | 34 | /** 35 | * web controller 36 | */ 37 | @Pointcut("execution(public * com.pphh.oauth.controller..*Controller.*(..))") 38 | public void webController() { 39 | } 40 | 41 | /** 42 | * oauth2 endpoint 43 | */ 44 | @Pointcut("execution(public * com.pphh.oauth.controller..*Endpoint.*(..))") 45 | public void oauth2Endpoint() { 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/config/IdentityProviderConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.config; 16 | 17 | import com.pphh.oauth.core.idp.AuthenticationProvider; 18 | import com.pphh.oauth.idp.local.LocalAuthenticationProvider; 19 | import org.springframework.context.annotation.Bean; 20 | import org.springframework.context.annotation.Configuration; 21 | 22 | /** 23 | * initializer of identity provider beans 24 | * 25 | * @author huangyinhuang 26 | * @date 7/2/2018 27 | */ 28 | @Configuration 29 | public class IdentityProviderConfig { 30 | 31 | @Bean("localIdpProvider") 32 | public AuthenticationProvider createLocalAuthenticationProvider() { 33 | return new LocalAuthenticationProvider(); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/config/LDAPConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.config; 16 | 17 | import com.pphh.oauth.utils.EnvProperty; 18 | import com.pphh.oauth.utils.LdapProperty; 19 | import org.springframework.beans.factory.annotation.Autowired; 20 | import org.springframework.context.annotation.Configuration; 21 | 22 | import java.util.ArrayList; 23 | import java.util.Arrays; 24 | import java.util.List; 25 | 26 | /** 27 | * ldap configuration 28 | * 29 | * @author huangyinhuang 30 | * @date 7/2/2018 31 | */ 32 | @Configuration 33 | public class LDAPConfiguration { 34 | 35 | /** 36 | * envProperty is used to fetch environment settings 37 | */ 38 | @Autowired 39 | private EnvProperty envProperty; 40 | 41 | /** 42 | * ldapProperty is implemented by ConfigurationProperties, which is initialized at app startup 43 | */ 44 | @Autowired 45 | private LdapProperty ldapProperty; 46 | 47 | public String getServer() { 48 | return envProperty.getProperty("app.ldap.server"); 49 | } 50 | 51 | public List getPaths() { 52 | String[] paths = null; 53 | if (ldapProperty.getSearch() != null) { 54 | paths = ldapProperty.getSearch().getPaths(); 55 | } 56 | 57 | if (paths != null) { 58 | return Arrays.asList(ldapProperty.getSearch().getPaths()); 59 | } else { 60 | return new ArrayList<>(); 61 | } 62 | 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/config/SwaggerConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.config; 16 | 17 | import org.springframework.context.annotation.Bean; 18 | import org.springframework.context.annotation.Configuration; 19 | import springfox.documentation.builders.ApiInfoBuilder; 20 | import springfox.documentation.builders.PathSelectors; 21 | import springfox.documentation.builders.RequestHandlerSelectors; 22 | import springfox.documentation.service.ApiInfo; 23 | import springfox.documentation.spi.DocumentationType; 24 | import springfox.documentation.spring.web.plugins.Docket; 25 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 26 | 27 | /** 28 | * swagger configuration 29 | * 30 | * @author huangyinhuang 31 | * @date 7/2/2018 32 | */ 33 | @Configuration 34 | @EnableSwagger2 35 | public class SwaggerConfig { 36 | 37 | @Bean 38 | public Docket createRestApi() { 39 | return new Docket(DocumentationType.SWAGGER_2) 40 | .apiInfo(apiInfo()) 41 | .select() 42 | .apis(RequestHandlerSelectors.basePackage("com.pphh.oauth.controller")) 43 | .paths(PathSelectors.any()) 44 | .build(); 45 | } 46 | 47 | private ApiInfo apiInfo() { 48 | return new ApiInfoBuilder() 49 | .title("统一认证和授权系统") 50 | .description("更多信息请联系pphh") 51 | .termsOfServiceUrl("http://demo.auth.com/") 52 | .version("1.0") 53 | .build(); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/config/UserAuditorAware.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.config; 16 | 17 | import com.pphh.oauth.utils.RequestContextUtil; 18 | import lombok.extern.slf4j.Slf4j; 19 | import org.springframework.context.annotation.Configuration; 20 | import org.springframework.data.domain.AuditorAware; 21 | import org.springframework.data.jpa.repository.config.EnableJpaAuditing; 22 | 23 | /** 24 | * initializer of user audit, which will be used by spring jpa audit feature 25 | * 26 | * @author huangyinhuang 27 | * @date 7/2/2018 28 | */ 29 | @Configuration 30 | @EnableJpaAuditing 31 | @Slf4j 32 | public class UserAuditorAware implements AuditorAware { 33 | 34 | public static final String DEFAULT_SYSTEM_NAME = "system"; 35 | 36 | @Override 37 | public String getCurrentAuditor() { 38 | String userName = RequestContextUtil.getCurrentUserName(); 39 | 40 | if (userName == null) { 41 | userName = DEFAULT_SYSTEM_NAME; 42 | } 43 | 44 | return userName; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/constant/SecurityActionType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.constant; 16 | 17 | /** 18 | * the enum of user security action 19 | * 20 | * @author huangyinhuang 21 | * @date 7/2/2018 22 | */ 23 | public enum SecurityActionType { 24 | 25 | /** 26 | * 用户登录 27 | */ 28 | LOGIN, 29 | 30 | /** 31 | * 用户更改密码 32 | */ 33 | CHANGE_PASSWORD 34 | 35 | } 36 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/controller/AuditController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.controller; 16 | 17 | import com.pphh.oauth.core.response.Response; 18 | import com.pphh.oauth.core.response.MessageType; 19 | import com.pphh.oauth.po.AuditLogEntity; 20 | import com.pphh.oauth.service.impl.AuditService; 21 | import com.pphh.oauth.vo.PageVO; 22 | import io.swagger.annotations.ApiOperation; 23 | import org.springframework.beans.factory.annotation.Autowired; 24 | import org.springframework.web.bind.annotation.RequestMapping; 25 | import org.springframework.web.bind.annotation.RequestMethod; 26 | import org.springframework.web.bind.annotation.RequestParam; 27 | import org.springframework.web.bind.annotation.RestController; 28 | 29 | 30 | /** 31 | * AuditController 32 | * 33 | * @author huangyinhuang 34 | * @date 7/5/2018 35 | */ 36 | @RestController 37 | @RequestMapping("/api/audit") 38 | public class AuditController { 39 | 40 | @Autowired 41 | private AuditService auditService; 42 | 43 | @ApiOperation(value = "获取分页audit_log列表") 44 | @RequestMapping(method = RequestMethod.GET) 45 | public Response> getAuditLogsByPage(@RequestParam Integer page, 46 | @RequestParam Integer size) { 47 | PageVO auditLogPageVO = auditService.fetchAuditLogsByPage(page, size); 48 | return Response.mark(MessageType.SUCCESS, auditLogPageVO); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/controller/ScopeMgtController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.controller; 16 | 17 | import com.pphh.oauth.core.response.MessageType; 18 | import com.pphh.oauth.core.response.Response; 19 | import io.swagger.annotations.ApiOperation; 20 | import org.springframework.web.bind.annotation.RequestMapping; 21 | import org.springframework.web.bind.annotation.RequestMethod; 22 | import org.springframework.web.bind.annotation.RestController; 23 | 24 | /** 25 | * OAuth2 Scope的后台管理接口 26 | * 27 | * @author huangyinhuang 28 | * @date 7/5/2018 29 | */ 30 | @RestController 31 | @RequestMapping("/api/scopes") 32 | public class ScopeMgtController { 33 | 34 | @ApiOperation(value = "获取OAuth2 Scope列表") 35 | @RequestMapping(method = RequestMethod.GET) 36 | public Response getScopeList() { 37 | return Response.mark(MessageType.SUCCESS, "fetch scope list"); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/dao/ApprovedScopeRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.dao; 16 | 17 | import com.pphh.oauth.po.ApprovedScopeEntity; 18 | import org.springframework.data.jpa.repository.Query; 19 | import org.springframework.data.repository.CrudRepository; 20 | 21 | /** 22 | * approved scope repository 23 | * 24 | * @author huangyinhuang 25 | * @date 7/3/2018 26 | */ 27 | public interface ApprovedScopeRepository extends CrudRepository { 28 | 29 | @Query("SELECT e FROM ApprovedScopeEntity e WHERE e.isActive=true and e.approvedSiteId=?1") 30 | Iterable findBySiteId(Long siteId); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/dao/ApprovedSiteRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.dao; 16 | 17 | import com.pphh.oauth.po.ApprovedSiteEntity; 18 | import org.springframework.data.domain.Page; 19 | import org.springframework.data.domain.Pageable; 20 | import org.springframework.data.jpa.domain.Specification; 21 | import org.springframework.data.jpa.repository.Modifying; 22 | import org.springframework.data.jpa.repository.Query; 23 | 24 | /** 25 | * approved site repository 26 | * 27 | * @author huangyinhuang 28 | * @date 7/3/2018 29 | */ 30 | public interface ApprovedSiteRepository extends BaseJpaRepository { 31 | 32 | @Query("SELECT e FROM ApprovedSiteEntity e WHERE e.isActive=true and e.userName=?1") 33 | Iterable findByUserName(String userName); 34 | 35 | @Modifying(clearAutomatically = true) 36 | @Query("update ApprovedSiteEntity e set e.isActive=false where e.id=?1") 37 | void removeById(Long id); 38 | 39 | @Query("SELECT e FROM ApprovedSiteEntity e WHERE e.isActive=true and e.userName=?1 and e.clientId=?2") 40 | Iterable findByUserNameAndClientId(String userName, String clientId); 41 | 42 | Page findAll(Specification specification, Pageable pageable); 43 | 44 | } 45 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/dao/AuditLogRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.dao; 16 | 17 | import com.pphh.oauth.po.AuditLogEntity; 18 | import org.springframework.data.domain.Page; 19 | import org.springframework.data.domain.Pageable; 20 | import org.springframework.data.jpa.repository.Modifying; 21 | import org.springframework.data.jpa.repository.Query; 22 | import org.springframework.data.repository.CrudRepository; 23 | 24 | import java.sql.Timestamp; 25 | 26 | /** 27 | * audit log repository 28 | * 29 | * @author huangyinhuang 30 | * @date 7/3/2018 31 | */ 32 | public interface AuditLogRepository extends CrudRepository { 33 | 34 | /** 35 | * 删除过期的审计日志,从数据库中永久删除字段 36 | * 37 | * @param expired 过期时间 38 | */ 39 | @Modifying(clearAutomatically = true) 40 | @Query("delete from AuditLogEntity e where e.updateTime<=?1") 41 | void deleteByTimeBefore(Timestamp expired); 42 | 43 | @Query("select e from AuditLogEntity e where e.isActive=true and e.userName is not null") 44 | Page findAll(Pageable pageable); 45 | 46 | } 47 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/dao/AuthenticationHolderRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.dao; 16 | 17 | import com.pphh.oauth.po.AuthenticationHolderEntity; 18 | 19 | /** 20 | * authentication holder repository 21 | * 22 | * @author huangyinhuang 23 | * @date 7/3/2018 24 | */ 25 | public interface AuthenticationHolderRepository extends BaseJpaRepository { 26 | } 27 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/dao/BaseJpaRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.dao; 16 | 17 | import org.springframework.data.jpa.repository.JpaRepository; 18 | import org.springframework.data.repository.NoRepositoryBean; 19 | 20 | import java.io.Serializable; 21 | 22 | /** 23 | * a base jpa repository 24 | * 25 | * @author huangyinhuang 26 | * @date 7/3/2018 27 | */ 28 | @NoRepositoryBean 29 | public interface BaseJpaRepository extends JpaRepository { 30 | 31 | Iterable findByIsActiveIsTrue(); 32 | 33 | default Iterable getAll() { 34 | return findByIsActiveIsTrue(); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/dao/ClientRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.dao; 16 | 17 | import com.pphh.oauth.po.ClientEntity; 18 | import com.pphh.oauth.po.UserEntity; 19 | import org.springframework.data.domain.Page; 20 | import org.springframework.data.domain.Pageable; 21 | import org.springframework.data.jpa.domain.Specification; 22 | import org.springframework.data.jpa.repository.Modifying; 23 | import org.springframework.data.jpa.repository.Query; 24 | 25 | import java.util.List; 26 | 27 | /** 28 | * client repository 29 | * 30 | * @author huangyinhuang 31 | * @date 7/3/2018 32 | */ 33 | public interface ClientRepository extends BaseJpaRepository { 34 | 35 | @Query("SELECT e FROM ClientEntity e WHERE e.isActive=true") 36 | List findAllEx(); 37 | 38 | @Query("SELECT e FROM ClientEntity e WHERE e.isActive=true and e.owner=?1") 39 | List findByOwner(UserEntity owner); 40 | 41 | @Query("SELECT e FROM ClientEntity e WHERE e.isActive=true and e.clientId=?1") 42 | ClientEntity findByIdEx(String clientId); 43 | 44 | @Query("SELECT e FROM ClientEntity e WHERE e.clientId=?1") 45 | ClientEntity findByClientId(String clientId); 46 | 47 | @Modifying(clearAutomatically = true) 48 | @Query("update ClientEntity e set e.isActive=false where e.id=?1") 49 | void removeById(Long id); 50 | 51 | Page findAll(Specification specification, Pageable pageable); 52 | 53 | @Query("select e from ClientEntity e where e.isActive=true") 54 | @Override 55 | Page findAll(Pageable pageable); 56 | 57 | } 58 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/dao/ClientScopeRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.dao; 16 | 17 | import com.pphh.oauth.po.ClientScopeEntity; 18 | import org.springframework.data.repository.CrudRepository; 19 | 20 | /** 21 | * client scope repository 22 | * 23 | * @author huangyinhuang 24 | * @date 7/3/2018 25 | */ 26 | public interface ClientScopeRepository extends CrudRepository { 27 | } 28 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/dao/SecurityActionRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.dao; 16 | 17 | import com.pphh.oauth.po.UserSecurityActionEntity; 18 | import org.springframework.data.jpa.repository.Query; 19 | 20 | /** 21 | * security action repository 22 | * 23 | * @author huangyinhuang 24 | * @date 7/3/2018 25 | */ 26 | public interface SecurityActionRepository extends BaseJpaRepository { 27 | 28 | @Query("SELECT e FROM UserSecurityActionEntity e WHERE e.isActive=true AND e.onceFlag=?1") 29 | UserSecurityActionEntity findAction(String onceFlag); 30 | 31 | @Query("SELECT e FROM UserSecurityActionEntity e WHERE e.isActive=true AND e.onceFlag=?1 AND e.userId=?2") 32 | UserSecurityActionEntity findAction(String onceFlag, Long userId); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/dao/UserRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.dao; 16 | 17 | import com.pphh.oauth.po.UserEntity; 18 | import org.springframework.data.domain.Page; 19 | import org.springframework.data.domain.Pageable; 20 | import org.springframework.data.jpa.domain.Specification; 21 | import org.springframework.data.jpa.repository.Query; 22 | import org.springframework.data.repository.query.Param; 23 | 24 | /** 25 | * user repository 26 | * 27 | * @author huangyinhuang 28 | * @date 7/3/2018 29 | */ 30 | public interface UserRepository extends BaseJpaRepository { 31 | 32 | Long countByName(String name); 33 | 34 | @Query("SELECT e FROM UserEntity e WHERE e.isActive=true AND e.name=?1") 35 | UserEntity findOneByName(String name); 36 | 37 | @Query("SELECT e FROM UserEntity e WHERE e.isActive=true AND e.email=?1") 38 | UserEntity findOneByEmail(String email); 39 | 40 | Page findAll(Specification specification, Pageable pageable); 41 | 42 | @Query("select e from UserEntity e where e.isActive=true") 43 | @Override 44 | Page findAll(Pageable pageable); 45 | 46 | @Query("select e from UserEntity e where e.isActive=true and e.name like concat('%',:name,'%')") 47 | Page fuzzyFindByName(@Param("name") String name, Pageable pageable); 48 | 49 | } 50 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/exception/UnAuthorizeException.java: -------------------------------------------------------------------------------- 1 | package com.pphh.oauth.exception; 2 | 3 | import com.pphh.oauth.core.response.MessageType; 4 | 5 | /** 6 | * UnAuthorizeException 7 | * 8 | * @author huangyinhuang 9 | * @date 7/3/2018 10 | */ 11 | public class UnAuthorizeException extends BaseException { 12 | 13 | public UnAuthorizeException(MessageType msgType) { 14 | super(msgType); 15 | } 16 | 17 | public UnAuthorizeException(MessageType msgType, Throwable cause) { 18 | super(msgType, cause); 19 | } 20 | 21 | public UnAuthorizeException(MessageType msgType, String message) { 22 | super(msgType, message); 23 | } 24 | 25 | public UnAuthorizeException(MessageType msgType, Throwable cause, String message) { 26 | super(msgType, cause, message); 27 | } 28 | 29 | public UnAuthorizeException(MessageType msgType, String details, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { 30 | super(msgType, details, cause, enableSuppression, writableStackTrace); 31 | } 32 | 33 | public static UnAuthorizeException newException(MessageType msgType, String message, Object... params) { 34 | UnAuthorizeException exception; 35 | if (params != null && params.length > 0) { 36 | String formatMessage = String.format(message, params); 37 | if (params[params.length - 1] instanceof Throwable) { 38 | exception = new UnAuthorizeException(msgType, (Throwable) params[params.length - 1], formatMessage); 39 | } else { 40 | exception = new UnAuthorizeException(msgType, formatMessage); 41 | } 42 | } else { 43 | exception = new UnAuthorizeException(msgType, message); 44 | } 45 | return exception; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/idp/local/LocalIdentityProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.idp.local; 16 | 17 | import com.pphh.oauth.core.idp.AuthenticationProvider; 18 | import com.pphh.oauth.core.idp.IdentityProvider; 19 | import com.pphh.oauth.core.idp.IdentityProviderConfiguration; 20 | 21 | /** 22 | * local identity provider 23 | * 24 | * @author huangyinhuang 25 | * @date 7/2/2018 26 | */ 27 | public class LocalIdentityProvider implements IdentityProvider { 28 | 29 | @Override 30 | public Class configuration() { 31 | return LocalProviderConfiguration.class; 32 | } 33 | 34 | @Override 35 | public Class authenticationProvider() { 36 | return LocalAuthenticationProvider.class; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/idp/local/LocalProviderConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.idp.local; 16 | 17 | import com.pphh.oauth.core.idp.IdentityProviderConfiguration; 18 | 19 | /** 20 | * Please add description here. 21 | * 22 | * @author huangyinhuang 23 | * @date 7/2/2018 24 | */ 25 | public class LocalProviderConfiguration implements IdentityProviderConfiguration { 26 | 27 | @Override 28 | public Class support() { 29 | return this.getClass(); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/po/ApprovedScopeEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.po; 16 | 17 | import lombok.Data; 18 | import lombok.EqualsAndHashCode; 19 | 20 | import javax.persistence.*; 21 | 22 | /** 23 | * Please add description here. 24 | * 25 | * @author huangyinhuang 26 | * @date 7/3/2018 27 | */ 28 | @Entity 29 | @Data 30 | @EqualsAndHashCode(callSuper = false) 31 | @Table(name = "approved_site_scope", schema = "", catalog = "") 32 | public class ApprovedScopeEntity extends BaseEntity { 33 | 34 | @Id 35 | @GeneratedValue(strategy = GenerationType.IDENTITY) 36 | @Column(name = "id", nullable = false) 37 | private Long id; 38 | 39 | @Basic 40 | @Column(name = "approved_site_id", nullable = false) 41 | private Long approvedSiteId; 42 | 43 | @Basic 44 | @Column(name = "scope_name", nullable = false, length = 256) 45 | private String scopeName; 46 | 47 | } 48 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/po/ApprovedSiteEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.po; 16 | 17 | import com.fasterxml.jackson.annotation.JsonInclude; 18 | import lombok.Data; 19 | import lombok.EqualsAndHashCode; 20 | 21 | import javax.persistence.*; 22 | 23 | /** 24 | * ApprovedSiteEntity 25 | * 26 | * @author huangyinhuang 27 | * @date 7/3/2018 28 | */ 29 | @Entity 30 | @Data 31 | @EqualsAndHashCode(callSuper = false) 32 | @Table(name = "approved_site", schema = "", catalog = "") 33 | public class ApprovedSiteEntity extends BaseEntity { 34 | 35 | @Id 36 | @GeneratedValue(strategy = GenerationType.IDENTITY) 37 | @Column(name = "id", nullable = false) 38 | private Long id; 39 | 40 | @Basic 41 | @Column(name = "client_id", nullable = false, length = 256) 42 | private String clientId; 43 | 44 | @Basic 45 | @Column(name = "user_name", nullable = false, length = 128) 46 | private String userName; 47 | 48 | @JsonInclude() 49 | @Transient 50 | private Iterable scopes; 51 | 52 | } 53 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/po/AuthenticationHolderEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.po; 16 | 17 | import lombok.Data; 18 | import lombok.EqualsAndHashCode; 19 | import org.springframework.security.oauth2.provider.OAuth2Authentication; 20 | import org.springframework.security.oauth2.provider.OAuth2Request; 21 | 22 | import javax.persistence.*; 23 | 24 | /** 25 | * AuthenticationHolderEntity 26 | * 27 | * @author huangyinhuang 28 | * @date 7/3/2018 29 | */ 30 | @Entity 31 | @Data 32 | @EqualsAndHashCode(callSuper = false) 33 | @Table(name = "authentication_holder", schema = "", catalog = "") 34 | public class AuthenticationHolderEntity extends BaseEntity { 35 | 36 | @Id 37 | @GeneratedValue(strategy = GenerationType.IDENTITY) 38 | @Column(name = "id", nullable = false) 39 | private Long id; 40 | 41 | @Basic 42 | @Column(name = "user_id", nullable = true) 43 | private Long userId; 44 | 45 | @Basic 46 | @Column(name = "approved", nullable = true) 47 | private Boolean approved; 48 | 49 | @Basic 50 | @Column(name = "redirect_uri", nullable = true, length = 2048) 51 | private String redirectUri; 52 | 53 | @Basic 54 | @Column(name = "client_id", nullable = true, length = 256) 55 | private String clientId; 56 | 57 | public void setAuthentication(OAuth2Authentication o2Authentication) { 58 | OAuth2Request o2Request = o2Authentication.getOAuth2Request(); 59 | this.setClientId(o2Request.getClientId()); 60 | this.setApproved(o2Request.isApproved()); 61 | this.setRedirectUri(o2Request.getRedirectUri()); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/po/AuthorizationCodeEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.po; 16 | 17 | import lombok.Data; 18 | import lombok.EqualsAndHashCode; 19 | 20 | import javax.persistence.*; 21 | import java.sql.Timestamp; 22 | 23 | /** 24 | * AuthorizationCodeEntity 25 | * 26 | * @author huangyinhuang 27 | * @date 7/3/2018 28 | */ 29 | @Entity 30 | @Data 31 | @EqualsAndHashCode(callSuper = false) 32 | @Table(name = "authorization_code", schema = "", catalog = "") 33 | public class AuthorizationCodeEntity extends BaseEntity { 34 | 35 | @Id 36 | @GeneratedValue(strategy = GenerationType.IDENTITY) 37 | @Column(name = "id", nullable = false) 38 | private Long id; 39 | 40 | @Basic 41 | @Column(name = "code", nullable = true, length = 256) 42 | private String code; 43 | 44 | @Basic 45 | @Column(name = "auth_holder_id", nullable = true) 46 | private Long authHolderId; 47 | 48 | @Basic 49 | @Column(name = "expiration", nullable = true) 50 | private Timestamp expiration; 51 | 52 | @Basic 53 | @Column(name = "user_id", nullable = true) 54 | private Long userId; 55 | 56 | @Basic 57 | @Column(name = "client_id", nullable = false) 58 | private String clientId; 59 | 60 | } 61 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/po/BaseEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.po; 16 | 17 | import lombok.Data; 18 | import org.springframework.data.annotation.CreatedBy; 19 | import org.springframework.data.annotation.LastModifiedBy; 20 | import org.springframework.data.jpa.domain.support.AuditingEntityListener; 21 | 22 | import javax.persistence.*; 23 | import java.util.Date; 24 | 25 | /** 26 | * BaseEntity 27 | * 28 | * @author huangyinhuang 29 | * @date 7/3/2018 30 | */ 31 | @Data 32 | @Cacheable(false) 33 | @EntityListeners(AuditingEntityListener.class) 34 | @MappedSuperclass 35 | public class BaseEntity { 36 | 37 | @Temporal(TemporalType.TIMESTAMP) 38 | @Column(name = "insert_time", insertable = false, updatable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP") 39 | public Date insertTime; 40 | 41 | @CreatedBy 42 | @Column(name = "insert_by", nullable = true, length = 64) 43 | public String insertBy; 44 | 45 | @Temporal(TemporalType.TIMESTAMP) 46 | @Column(name = "update_time", insertable = false, updatable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP") 47 | public Date updateTime; 48 | 49 | @LastModifiedBy 50 | @Column(name = "update_by", nullable = true, length = 64) 51 | public String updateBy; 52 | 53 | @Column(name = "is_active", nullable = false, columnDefinition = "TINYINT(1)") 54 | public Boolean isActive = true; 55 | 56 | } 57 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/po/ClientEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.po; 16 | 17 | import lombok.Data; 18 | import lombok.EqualsAndHashCode; 19 | 20 | import javax.persistence.*; 21 | 22 | /** 23 | * ClientEntity 24 | * 25 | * @author huangyinhuang 26 | * @date 7/3/2018 27 | */ 28 | @Entity 29 | @Data 30 | @EqualsAndHashCode(callSuper = false) 31 | @Table(name = "client", schema = "", catalog = "") 32 | public class ClientEntity extends BaseEntity { 33 | 34 | @Id 35 | @GeneratedValue(strategy = GenerationType.IDENTITY) 36 | @Column(name = "id", nullable = false) 37 | private Long id; 38 | 39 | @Basic 40 | @Column(name = "description", nullable = true, length = 1024) 41 | private String description; 42 | 43 | @Basic 44 | @Column(name = "client_id", nullable = true, length = 256) 45 | private String clientId; 46 | 47 | @Basic 48 | @Column(name = "client_secret", nullable = true, length = 2048) 49 | private String clientSecret; 50 | 51 | @Basic 52 | @Column(name = "basic_auth", nullable = true, length = 256) 53 | private String basicAuth; 54 | 55 | @Basic 56 | @Column(name = "redirect_url", nullable = true, length = 2048) 57 | private String redirectUrl; 58 | 59 | @ManyToOne(targetEntity = UserEntity.class) 60 | @JoinColumn(name = "owner_id") 61 | private UserEntity owner; 62 | 63 | @Basic 64 | @Column(name = "reuse_refresh_tokens", nullable = false) 65 | private Boolean reuseRefreshTokens = false; 66 | 67 | } 68 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/po/ClientScopeEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.po; 16 | 17 | import lombok.Data; 18 | import lombok.EqualsAndHashCode; 19 | 20 | import javax.persistence.*; 21 | 22 | /** 23 | * ClientScopeEntity 24 | * 25 | * @author huangyinhuang 26 | * @date 7/3/2018 27 | */ 28 | @Entity 29 | @Data 30 | @EqualsAndHashCode(callSuper = false) 31 | @Table(name = "client_scope", schema = "", catalog = "") 32 | public class ClientScopeEntity extends BaseEntity { 33 | 34 | @Id 35 | @GeneratedValue(strategy = GenerationType.IDENTITY) 36 | @Column(name = "id", nullable = false) 37 | private Long id; 38 | 39 | @Basic 40 | @Column(name = "client_id", nullable = false, length = 256) 41 | private String clientId; 42 | 43 | @Basic 44 | @Column(name = "scope_name", nullable = false, length = 256) 45 | private String scopeName; 46 | 47 | } 48 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/po/RefreshTokenEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.po; 16 | 17 | import lombok.Data; 18 | import lombok.EqualsAndHashCode; 19 | import org.springframework.security.oauth2.common.OAuth2RefreshToken; 20 | 21 | import javax.persistence.*; 22 | import java.sql.Timestamp; 23 | 24 | /** 25 | * RefreshTokenEntity 26 | * 27 | * @author huangyinhuang 28 | * @date 7/3/2018 29 | */ 30 | @Entity 31 | @Data 32 | @EqualsAndHashCode(callSuper = false) 33 | @Table(name = "refresh_token", schema = "", catalog = "") 34 | public class RefreshTokenEntity extends BaseEntity implements OAuth2RefreshToken { 35 | 36 | @Id 37 | @GeneratedValue(strategy = GenerationType.IDENTITY) 38 | @Column(name = "id", nullable = false) 39 | private Long id; 40 | 41 | @Basic 42 | @Column(name = "token_value", nullable = true, length = 1024) 43 | private String tokenValue; 44 | 45 | @Basic 46 | @Column(name = "expiration", nullable = true) 47 | private Timestamp expiration; 48 | 49 | @Basic 50 | @Column(name = "auth_holder_id", nullable = true) 51 | private Long authHolderId; 52 | 53 | @Basic 54 | @Column(name = "user_id", nullable = true) 55 | private Long userId; 56 | 57 | @Basic 58 | @Column(name = "client_id", nullable = false) 59 | private String clientId; 60 | 61 | @Override 62 | public String getValue() { 63 | return this.tokenValue; 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/po/UserEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.po; 16 | 17 | import lombok.Data; 18 | import lombok.EqualsAndHashCode; 19 | 20 | import javax.persistence.*; 21 | import java.util.Date; 22 | 23 | /** 24 | * UserEntity 25 | * 26 | * @author huangyinhuang 27 | * @date 7/3/2018 28 | */ 29 | @Entity 30 | @Data 31 | @Cacheable(false) 32 | @EqualsAndHashCode(callSuper = false) 33 | @Table(name = "user", schema = "", catalog = "") 34 | public class UserEntity extends BaseEntity { 35 | 36 | @Id 37 | @GeneratedValue(strategy = GenerationType.IDENTITY) 38 | @Column(name = "id", nullable = false) 39 | private Long id; 40 | 41 | @Column(name = "name", nullable = false) 42 | private String name; 43 | 44 | @Column(name = "email", nullable = false) 45 | private String email; 46 | 47 | @Column(name = "password", nullable = true) 48 | private String password; 49 | 50 | @Column(name = "checkcode", nullable = true) 51 | private String checkcode; 52 | 53 | @Column(name = "roles", nullable = true) 54 | private String roles; 55 | 56 | @Temporal(TemporalType.TIMESTAMP) 57 | @Column(name = "last_visit_at", nullable = false) 58 | private Date lastVisitAt; 59 | 60 | } 61 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/po/UserSecurityActionEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.po; 16 | 17 | import com.pphh.oauth.constant.SecurityActionType; 18 | import lombok.Data; 19 | import lombok.EqualsAndHashCode; 20 | 21 | import javax.persistence.*; 22 | 23 | /** 24 | * UserSecurityActionEntity 25 | * 26 | * @author huangyinhuang 27 | * @date 7/3/2018 28 | */ 29 | @Entity 30 | @Data 31 | @Cacheable(false) 32 | @EqualsAndHashCode(callSuper = false) 33 | @Table(name = "user_security_action") 34 | public class UserSecurityActionEntity extends BaseEntity { 35 | 36 | @Id 37 | @GeneratedValue(strategy = GenerationType.IDENTITY) 38 | @Column(name = "id", nullable = false) 39 | private Long id; 40 | 41 | @Column(name = "type", nullable = false) 42 | @Enumerated(EnumType.STRING) 43 | private SecurityActionType type; 44 | 45 | @Column(name = "once_flag", nullable = false) 46 | private String onceFlag; 47 | 48 | @Column(name = "user_id", nullable = true) 49 | private Long userId; 50 | 51 | @Column(name = "user_name", nullable = true) 52 | private String userName; 53 | 54 | } 55 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/service/AuthHolderService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.service; 16 | 17 | /** 18 | * AuthHolderService 19 | * 20 | * @author huangyinhuang 21 | * @date 7/3/2018 22 | */ 23 | public interface AuthHolderService { 24 | 25 | /** 26 | * get user name by an auth holder 27 | * 28 | * @param holderId auth holder id 29 | * @return user name 30 | */ 31 | String getUserNameByHolderId(Long holderId); 32 | 33 | /** 34 | * get client name by an auth holder 35 | * 36 | * @param holderId auth holder id 37 | * @return client name 38 | */ 39 | String getClientNameByHolderId(Long holderId); 40 | 41 | } 42 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/service/ClientService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.service; 16 | 17 | import com.pphh.oauth.po.ClientEntity; 18 | import com.pphh.oauth.po.UserEntity; 19 | import com.pphh.oauth.vo.ClientVO; 20 | import com.pphh.oauth.vo.PageVO; 21 | 22 | import java.util.List; 23 | 24 | /** 25 | * ClientService 26 | * 27 | * @author huangyinhuang 28 | * @date 7/3/2018 29 | */ 30 | public interface ClientService { 31 | 32 | void register(ClientVO clientVO); 33 | 34 | List fetchAllClients(); 35 | 36 | PageVO fetchClientsByPage(String clientId, Long ownerId, int page, int size); 37 | 38 | List fetchClientByUser(UserEntity owner); 39 | 40 | void removeById(Long clientId); 41 | 42 | void updateById(ClientEntity client); 43 | 44 | String getName(Long clientId); 45 | 46 | ClientEntity findByClientName(String clientName); 47 | 48 | } 49 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/service/LdapService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.service; 16 | 17 | import com.pphh.oauth.vo.UserVO; 18 | 19 | 20 | /** 21 | * Please add description here. 22 | * 23 | * @author huangyinhuang 24 | * @date 7/3/2018 25 | */ 26 | 27 | public interface LdapService { 28 | 29 | /** 30 | * 通过LDAP校验用户登录 31 | * 32 | * @param username 用户LDAP域账号 33 | * @param password 用户LDAP域账号密码 34 | * @return 用户信息 35 | */ 36 | public UserVO login(String username, String password); 37 | 38 | } 39 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/service/MailService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.service; 16 | 17 | /** 18 | * MailService 19 | * 20 | * @author huangyinhuang 21 | * @date 7/3/2018 22 | */ 23 | public interface MailService { 24 | 25 | /** 26 | * 发送邮件 27 | * 28 | * @param from 发送者 29 | * @param to 接受者 30 | * @param subject 邮件标题 31 | * @param content 邮件内容 32 | * @return 33 | */ 34 | Boolean sendMail(String from, String to, String subject, String content); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/service/MetricService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.service; 16 | 17 | import com.pphh.oauth.core.constant.GrantType; 18 | import org.springframework.stereotype.Service; 19 | 20 | import java.util.HashMap; 21 | import java.util.Map; 22 | 23 | /** 24 | * 服务运营指标数据收集,包括登录次数,授权次数,授权校验次数等 25 | * 26 | * @author huangyinhuang 27 | * @date 7/3/2018 28 | */ 29 | public interface MetricService { 30 | 31 | public void recordLogin(String userName); 32 | 33 | public void recordAuthCode(String userName, String clientId); 34 | 35 | public void recordAuthToken(String userName, String clientId, GrantType type); 36 | 37 | public void recordIntrospect(); 38 | 39 | public void recordRevoke(); 40 | 41 | } 42 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/service/OAuth2Service.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.service; 16 | 17 | import com.pphh.oauth.vo.AuthCodeVO; 18 | import com.pphh.oauth.vo.ClientVO; 19 | import com.pphh.oauth.vo.ValidityVO; 20 | import org.springframework.security.oauth2.common.OAuth2AccessToken; 21 | 22 | /** 23 | * OAuth2Service 24 | * 25 | * @author huangyinhuang 26 | * @date 7/3/2018 27 | */ 28 | public interface OAuth2Service { 29 | 30 | AuthCodeVO authorize(String userName, ClientVO clientVO); 31 | 32 | /** 33 | * 通过授权码获取access/refresh token 34 | * 35 | * @param code 授权码 36 | * @param clientId 申请token的client id 37 | * @param clientSecret 申请token的client secret 38 | * @return 39 | */ 40 | OAuth2AccessToken issueToken(String code, String clientId, String clientSecret); 41 | 42 | /** 43 | * 通过用户名和密码获取access/refresh token 44 | * 45 | * @param username 用户名 46 | * @param password 用户密码 47 | * @param clientId 申请token的client id 48 | * @param clientSecret 申请token的client secret 49 | * @return 50 | */ 51 | OAuth2AccessToken issueToken(String username, String password, String clientId, String clientSecret); 52 | 53 | /** 54 | * 通过client id/secret直接获取access/refresh token 55 | * 56 | * @param clientId 申请token的client id 57 | * @param clientSecret 申请token的client secret 58 | * @return 59 | */ 60 | OAuth2AccessToken issueToken(String clientId, String clientSecret); 61 | 62 | OAuth2AccessToken refreshToken(String token, String clientId, String clientSecret); 63 | 64 | ValidityVO introspectToken(String token); 65 | 66 | Boolean revokeToken(String token); 67 | 68 | } 69 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/service/impl/AuditService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.service.impl; 16 | 17 | import com.pphh.oauth.dao.AuditLogRepository; 18 | import com.pphh.oauth.exception.BaseException; 19 | import com.pphh.oauth.po.AuditLogEntity; 20 | import com.pphh.oauth.vo.PageVO; 21 | import lombok.extern.slf4j.Slf4j; 22 | import org.springframework.beans.factory.annotation.Autowired; 23 | import org.springframework.data.domain.Page; 24 | import org.springframework.data.domain.PageRequest; 25 | import org.springframework.data.domain.Pageable; 26 | import org.springframework.stereotype.Service; 27 | import org.springframework.transaction.annotation.Transactional; 28 | 29 | /** 30 | * 审计服务:记录各种操作日志,用于后续审计 31 | * 32 | * @author huangyinhuang 33 | * @date 7/3/2018 34 | */ 35 | @Service 36 | @Slf4j 37 | public class AuditService { 38 | 39 | @Autowired 40 | AuditLogRepository auditLogRepo; 41 | 42 | @Transactional(rollbackFor = BaseException.class) 43 | public void recordOperation(AuditLogEntity actionItem) { 44 | auditLogRepo.save(actionItem); 45 | } 46 | 47 | public PageVO fetchAuditLogsByPage(int page, int size) { 48 | PageVO pageVO = new PageVO<>(); 49 | Pageable pageable = new PageRequest(page, size); 50 | Page auditLogPage = auditLogRepo.findAll(pageable); 51 | pageVO.setContent(auditLogPage.getContent()); 52 | pageVO.setTotalElements(auditLogPage.getTotalElements()); 53 | return pageVO; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/utils/ConvertUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.utils; 16 | 17 | import com.pphh.oauth.core.response.MessageType; 18 | import com.pphh.oauth.exception.BaseException; 19 | import org.springframework.beans.BeanUtils; 20 | 21 | import java.util.List; 22 | import java.util.function.Function; 23 | import java.util.stream.Collectors; 24 | import java.util.stream.StreamSupport; 25 | 26 | /** 27 | * The utils to convert object between class/types 28 | * 29 | * @author huangyinhuang 30 | * @date 7/2/2018 31 | */ 32 | public class ConvertUtil { 33 | 34 | public static T convert(S s, Class tClass) { 35 | try { 36 | T t = tClass.newInstance(); 37 | BeanUtils.copyProperties(s, t); 38 | return t; 39 | } catch (Exception e) { 40 | throw BaseException.newException(MessageType.ERROR, "convert error"); 41 | } 42 | } 43 | 44 | public static T convert(S s, T t) { 45 | try { 46 | BeanUtils.copyProperties(s, t); 47 | return t; 48 | } catch (Exception e) { 49 | throw BaseException.newException(MessageType.ERROR, "convert error"); 50 | } 51 | } 52 | 53 | public static List convert(Iterable iterable, Class tClass) { 54 | return StreamSupport.stream(iterable.spliterator(), false) 55 | .map(s -> ConvertUtil.convert(s, tClass)).collect(Collectors.toList()); 56 | } 57 | 58 | public static List convert(Iterable iterable, Function mapper) { 59 | return StreamSupport.stream(iterable.spliterator(), false).map(mapper).collect(Collectors.toList()); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/utils/EnvProperty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.utils; 16 | 17 | import lombok.extern.slf4j.Slf4j; 18 | import org.springframework.beans.factory.annotation.Autowired; 19 | import org.springframework.beans.factory.annotation.Value; 20 | import org.springframework.core.env.Environment; 21 | import org.springframework.stereotype.Component; 22 | 23 | /** 24 | * a util to read environment property 25 | * 26 | * @author huangyinhuang 27 | * @date 7/2/2018 28 | */ 29 | @Component 30 | @Slf4j 31 | public class EnvProperty { 32 | 33 | public static String HEADER_AUDIT_USERNAME = "app.audit.username"; 34 | 35 | @Value("${app.jwt.check.enable:true}") 36 | public Boolean JWT_CHECK_ENABLE; 37 | 38 | @Value("${app.jwt.sign.secret:secret}") 39 | public String JWT_SIGN_SECRET; 40 | 41 | @Value("${app.jwt.sign.issuer:oauth2}") 42 | public String JWT_SIGN_ISSUER; 43 | 44 | @Value("${app.jwt.sign.expires:168}") 45 | public Integer JWT_SIGN_EXPIRES; 46 | 47 | @Value("${app.jwt.check.skip.uri:}") 48 | public String JWT_CHECK_SKIP_URI; 49 | 50 | @Value("${app.oauth.expiration.code:600000}") 51 | public Integer OAUTH_EXPIRE_CODE; 52 | 53 | @Value("${app.oauth.expiration.refreshtoken:3600000}") 54 | public Integer OAUTH_EXPIRE_REFRESH; 55 | 56 | @Value("${app.oauth.expiration.access:3600000}") 57 | public Integer OAUTH_EXPIRE_ACCESS; 58 | 59 | @Autowired 60 | private Environment environment; 61 | 62 | public String getProperty(String property) { 63 | return environment.getProperty(property); 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/utils/LdapProperty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.utils; 16 | 17 | import org.springframework.boot.context.properties.ConfigurationProperties; 18 | import org.springframework.stereotype.Component; 19 | 20 | /** 21 | * Use ConfigurationProperties to read a array of ldap search paths. 22 | * 23 | * @author huangyinhuang 24 | * @date 7/2/2018 25 | */ 26 | @Component 27 | @ConfigurationProperties(prefix = "app.ldap") 28 | public class LdapProperty { 29 | 30 | private Search search; 31 | 32 | public Search getSearch() { 33 | return search; 34 | } 35 | 36 | public void setSearch(Search search) { 37 | this.search = search; 38 | } 39 | 40 | public static class Search { 41 | public String[] paths; 42 | 43 | public String[] getPaths() { 44 | return paths; 45 | } 46 | 47 | public void setPaths(String[] paths) { 48 | this.paths = paths; 49 | } 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/utils/RequestContextUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.utils; 16 | 17 | import lombok.extern.slf4j.Slf4j; 18 | import org.springframework.web.context.request.RequestContextHolder; 19 | import org.springframework.web.context.request.ServletRequestAttributes; 20 | 21 | /** 22 | * a utils to read user name from request context 23 | * 24 | * @author huangyinhuang 25 | * @date 7/2/2018 26 | */ 27 | @Slf4j 28 | public class RequestContextUtil { 29 | 30 | public static String getCurrentUserName() { 31 | String userName = null; 32 | 33 | try { 34 | ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); 35 | userName = (String) requestAttributes.getAttribute(EnvProperty.HEADER_AUDIT_USERNAME, 0); 36 | } catch (Exception e) { 37 | log.info("Not able to read the user name by servlet requests. Probably it's a system call."); 38 | } 39 | 40 | return userName; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/vo/AuthCodeVO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.vo; 16 | 17 | import lombok.Data; 18 | 19 | import java.util.Date; 20 | 21 | /** 22 | * Please add description here. 23 | * 24 | * @author huangyinhuang 25 | * @date 7/3/2018 26 | */ 27 | @Data 28 | public class AuthCodeVO { 29 | Long id; 30 | String code; 31 | String redirectUrl; 32 | String clientName; 33 | String userName; 34 | Date expiration; 35 | Date insertTime; 36 | } 37 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/vo/AuthTokenVO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.vo; 16 | 17 | import lombok.Data; 18 | 19 | import java.util.Date; 20 | 21 | /** 22 | * AuthTokenVO 23 | * 24 | * @author huangyinhuang 25 | * @date 7/3/2018 26 | */ 27 | @Data 28 | public class AuthTokenVO { 29 | Long id; 30 | String value; 31 | String clientName; 32 | String userName; 33 | Date expiration; 34 | Date insertTime; 35 | } 36 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/vo/ClientCheckResultVO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.vo; 16 | 17 | import lombok.Data; 18 | 19 | /** 20 | * ClientCheckResultVO 21 | * 22 | * @author huangyinhuang 23 | * @date 7/3/2018 24 | */ 25 | @Data 26 | public class ClientCheckResultVO { 27 | Boolean isValid; 28 | Boolean directApprove; 29 | } 30 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/vo/ClientVO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.vo; 16 | 17 | import lombok.Data; 18 | 19 | import java.util.Set; 20 | 21 | /** 22 | * ClientVO 23 | * 24 | * @author huangyinhuang 25 | * @date 7/3/2018 26 | */ 27 | @Data 28 | public class ClientVO { 29 | private Long id; 30 | private String respType; 31 | private String description; 32 | private String clientId; 33 | private String clientSecret; 34 | private String basicAuth; 35 | private String redirectUrl; 36 | private String ownerName; 37 | private Set scopes; 38 | private String rememberChoice; 39 | } 40 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/vo/GrantRequestVO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.vo; 16 | 17 | import com.pphh.oauth.core.constant.GrantType; 18 | import lombok.Data; 19 | 20 | /** 21 | * GrantRequestVO 22 | * 23 | * @author huangyinhuang 24 | * @date 7/3/2018 25 | */ 26 | @Data 27 | public class GrantRequestVO { 28 | GrantType grant_type; 29 | String code; 30 | String refresh_token; 31 | 32 | public GrantType getGrantType() { 33 | return grant_type; 34 | } 35 | 36 | public String getCode() { 37 | return code; 38 | } 39 | 40 | public String getRefreshToken() { 41 | return refresh_token; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/vo/PageVO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.vo; 16 | 17 | import lombok.Data; 18 | 19 | import java.util.List; 20 | 21 | /** 22 | * page view object 23 | * 24 | * @author huangyinhuang 25 | * @date 7/3/2018 26 | */ 27 | @Data 28 | public class PageVO { 29 | private List content; 30 | private Long totalElements; 31 | } 32 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/vo/UserVO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.vo; 16 | 17 | import lombok.Data; 18 | 19 | import java.util.Date; 20 | 21 | /** 22 | * UserVO 23 | * 24 | * @author huangyinhuang 25 | * @date 7/3/2018 26 | */ 27 | @Data 28 | public class UserVO { 29 | Long id; 30 | String name; 31 | String email; 32 | Date lastVisitAt; 33 | Date insertTime; 34 | } 35 | -------------------------------------------------------------------------------- /oauth-server/src/main/java/com/pphh/oauth/vo/ValidityVO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 peipeihh 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * limitations under the License. 14 | */ 15 | package com.pphh.oauth.vo; 16 | 17 | import lombok.Data; 18 | 19 | /** 20 | * ValidityVO 21 | * 22 | * @author huangyinhuang 23 | * @date 7/3/2018 24 | */ 25 | @Data 26 | public class ValidityVO { 27 | Boolean isValid; 28 | 29 | public ValidityVO() { 30 | this.isValid = Boolean.FALSE; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /oauth-server/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # server 2 | server.port = 8090 3 | 4 | # disable spring security 5 | security.basic.enabled = false 6 | management.security.enabled = false 7 | 8 | # app settings 9 | spring.application.name = auth2-server 10 | 11 | # data source 12 | spring.datasource.url = jdbc:mysql://localhost:3306/simple_oauth?useUnicode=true&characterEncoding=utf-8&useSSL=false 13 | spring.datasource.username = root 14 | spring.datasource.password = root 15 | spring.datasource.initialize = true 16 | spring.jpa.show-sql = true 17 | 18 | # simple admin configuration 19 | # jwt check flag 20 | app.jwt.check.enable = true 21 | app.jwt.sign.secret = secret 22 | app.jwt.sign.issuer = oauth-server 23 | app.jwt.sign.expires = 168 24 | app.jwt.check.skip.uri = /api/account/login,/api/account/refreshPassword,/api/account/register,/oauth2/token,/oauth2/revoke,/oauth2/introspect,/oauth2/docker/token,/api/clients/introspect 25 | 26 | # oauth 27 | # auth code的过期时间:10分钟(10*60*1000=600000) 28 | # access token的过期时间:6小时(6*60*60*1000=21600000) 29 | # refresh token的过期时间:3天(3*24*60*60*1000=259200000) 30 | app.oauth.expiration.code = 600000 31 | app.oauth.expiration.access = 21600000 32 | app.oauth.expiration.refreshtoken = 259200000 33 | 34 | # scheduler,其中timeout一般配置为fixedRate的三倍 35 | # Token软删除:每隔5分钟 36 | # Token硬删除:每天下午13点05分 37 | app.scheduler.enable = true 38 | app.scheduler.token.removal = 0 0/5 * * * ? 39 | app.scheduler.token.clean = 0 5 13 * * ? 40 | 41 | # docker registry with fixed client id/secret 42 | app.docker.registry.clientId = docker 43 | app.docker.registry.clientSecret = docker_secret 44 | -------------------------------------------------------------------------------- /sample/demo-front-jquery/README.md: -------------------------------------------------------------------------------- 1 | 2 | ## 1. 简介 3 | 4 | 这是一个演示项目,使用纯前端技术栈(html+jquery+css),对接Simple-OAuth所提供的oauth 2.0 implicit grant授权方式,实现用户登录功能。 5 | 6 | ## 2. 项目的结构和构建 7 | 8 | ``` 9 | - README.md 使用说明文档 10 | - package.json NPM构建配置文件 11 | - app.js express服务器启动配置文件 12 | + src 13 | - index.html 前端应用主页面 14 | - callback.html 登录后回调页面 15 | ``` 16 | 17 | #### 2.1 安装node并配置npm源 18 | 19 | 登录node官方网站安装node 3.10.8+。 20 | 21 | 配置npm源为淘宝源 22 | 23 | ``` 24 | npm set registry "https://registry.npm.taobao.org/" 25 | ``` 26 | 27 | 配置后可以通过npm config list查看。 28 | 29 | #### 2.2 前端项目构建运行命令 30 | 31 | 构建文件:./package.json 32 | 33 | 下载依赖包:npm install,执行成功后将会把express依赖包下载到/node_modules目录。 34 | 35 | 运行命令:node app.js,运行成功后可以通过[http://localhost:9001](http://localhost:9001)访问应用 36 | 37 | 注:应用的启动端口在./app.js文件中进行配置。 38 | 39 | ## 3. 演示 40 | 41 | 1. 准备工作 42 | - 根据oauth-server项目的README文档,启动simple oauth后端web服务。 43 | - 根据oauth-front项目的README文档,启动simple oauth前端web服务。 44 | - 若以项目缺省配置,simple oauth前端和后端分别启动在如下两个地址, 45 | ``` 46 | 前端服务 http://localhost 47 | 后端服务 http://localhost:8090 48 | ``` 49 | - 本演示项目将根据上面的两个地址进行授权登录跳转。 50 | 51 | 2. 注册应用 52 | - 请登录oauth前端页面,应该能看到auth server已经注册一个缺省的demo应用。 53 | * 应用查看列表:[http://localhost/#/dev/myclient](http://localhost/#/dev/myclient) 54 | 55 | - 若没有发现demo应用,可以手动注册应用, 56 | * client id = demo 57 | * 重定向返回地址为:.* (其含义为simple oauth接受demo client指定的任何返回地址) 58 | 59 | 3. 启动当前演示项目 60 | - 执行前端应用运行命令:node app.js 61 | - 打开浏览器,访问前端应用地址:http://localhost:9001 62 | - 点击登录按钮,将跳转到simple oauth的授权界面:http://localhost/#/authorize,点击同意按钮。 63 | * 注:若simple oauth没有登录,则需要先登录,再点击同意授权按钮,登录账号缺省为admin/admin,详情请查看simple auth项目readme文件。 64 | - 若一切正常,simple oauth将跳转回当前前端演示应用地址,并完成登录,显示登录账号。 65 | -------------------------------------------------------------------------------- /sample/demo-front-jquery/app.js: -------------------------------------------------------------------------------- 1 | let express = require('express'); 2 | let app = express(); 3 | app.use(express.static('src', {'index': 'index.html'})); 4 | 5 | // app.get('/api/hello', function (req, res) { 6 | // res.send('Hello World!'); 7 | // }); 8 | 9 | app.listen(9001); 10 | console.log("server is started, please open following url in the browser: "); 11 | console.log("http://localhost:9001"); -------------------------------------------------------------------------------- /sample/demo-front-jquery/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "demo-front-jquery", 3 | "version": "1.0.0", 4 | "description": "this is a simple web front project with jquery, interact with oauth 2.0 - implicit grant mode.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "pphh", 10 | "license": "Apache License 2.0", 11 | "dependencies": { 12 | "express": "^4.15.3" 13 | } 14 | } -------------------------------------------------------------------------------- /sample/demo-front-jquery/src/callback.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 登录回调页面 6 | 7 | 8 | 9 |

登录回调页面

10 |

登录中......

11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 39 | -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/front-vue/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "es2015", 4 | "stage-2" 5 | ], 6 | "plugins": [ 7 | "transform-runtime", 8 | "transform-vue-jsx" 9 | ], 10 | "comments": true 11 | } 12 | -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/front-vue/.postcssrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | "plugins": { 3 | "autoprefixer": {} 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/front-vue/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "front-vue", 3 | "version": "1.0.0", 4 | "description": "this is a simple demo of oauth.", 5 | "main": "webpack.config.js", 6 | "scripts": { 7 | "dev": "webpack-dev-server --inline --hot --env.dev", 8 | "build": "webpack -p --progress --hide-modules" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git@gitee.com:pphh/simple-oauth2.git" 13 | }, 14 | "keywords": [ 15 | "auth 2.0" 16 | ], 17 | "author": "pphh", 18 | "license": "Apache License 2.0", 19 | "dependencies": { 20 | "axios": "^0.16.2", 21 | "echarts": "^3.5.0", 22 | "element-ui": "^2.0.9", 23 | "jwt-decode": "^2.1.0", 24 | "vue": "^2.5.2", 25 | "vue-router": "^2.0.0", 26 | "vuex": "^2.3.1" 27 | }, 28 | "devDependencies": { 29 | "babel-core": "^6.0.0", 30 | "babel-loader": "^6.0.0", 31 | "babel-plugin-component": "^0.9.1", 32 | "babel-preset-env": "^1.6.1", 33 | "babel-preset-es2015": "^6.24.1", 34 | "babel-preset-stage-2": "^6.24.1", 35 | "babel-preset-vue-app": "^1.2.0", 36 | "cross-env": "^1.0.6", 37 | "css-loader": "^0.23.1", 38 | "file-loader": "^0.8.5", 39 | "html-webpack-plugin": "^2.24.1", 40 | "jsonwebtoken": "^7.3.0", 41 | "postcss-loader": "^1.3.3", 42 | "request": "^2.79.0", 43 | "style-loader": "^0.13.1", 44 | "uglifyjs-webpack-plugin": "^0.4.6", 45 | "url-loader": "^0.5.8", 46 | "vue-loader": "^9.8.0", 47 | "vue-style-loader": "^2.0.0", 48 | "webpack": "^2.4.5", 49 | "webpack-dev-server": "^2.4.5" 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/front-vue/src/api/index.js: -------------------------------------------------------------------------------- 1 | import axios from 'axios' 2 | import service from './service' 3 | 4 | axios.defaults.timeout = 60000; 5 | axios.defaults.headers.common['Content-Type'] = 'application/json'; 6 | axios.defaults.headers.post['Content-Type'] = 'application/json'; 7 | axios.defaults.headers.put['Content-Type'] = 'application/json'; 8 | 9 | export const api = service; -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/front-vue/src/api/restApi.js: -------------------------------------------------------------------------------- 1 | import axios from 'axios' 2 | 3 | export default { 4 | 5 | doGetRequest(url){ 6 | return axios.get(url) 7 | .then((response) => Promise.resolve(response)) 8 | .catch((error) => Promise.reject(error)) 9 | }, 10 | doDeleteRequest(url){ 11 | return axios.delete(url) 12 | .then((response) => Promise.resolve(response)) 13 | .catch((error) => Promise.reject(error)) 14 | }, 15 | doPutRequest(url, data){ 16 | if (typeof(data) == "object") { 17 | data = JSON.stringify(data); 18 | } 19 | return axios.put(url, data) 20 | .then((response) => Promise.resolve(response)) 21 | .catch((error) => Promise.reject(error)) 22 | }, 23 | doPostRequest(url, data){ 24 | if (typeof(data) == "object") { 25 | data = JSON.stringify(data); 26 | } 27 | return axios.post(url, data) 28 | .then((response) => Promise.resolve(response)) 29 | .catch((error) => Promise.reject(error)) 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/front-vue/src/api/service/authService.js: -------------------------------------------------------------------------------- 1 | import restApi from '../restApi' 2 | 3 | export default { 4 | fetchToken(request = {}) { 5 | let url = 'api/oauth2/accessToken?code=' + request.code; 6 | return restApi.doGetRequest(url); 7 | }, 8 | refreshToken(request = {}) { 9 | let url = 'api/oauth2/refreshToken?refresh_token=' + request.refresh_token; 10 | return restApi.doGetRequest(url); 11 | }, 12 | revokeToken(request = {}) { 13 | let url = 'api/oauth2/revokeToken?token=' + request.token; 14 | return restApi.doGetRequest(url); 15 | }, 16 | fetchLoginUrl(request = {}) { 17 | let url = null; 18 | if (request != null && request.callback != null) { 19 | url = 'api/oauth2/redirectUrl?callback=' + encodeURIComponent(request.callback); 20 | } else { 21 | url = 'api/oauth2/redirectUrl'; 22 | } 23 | return restApi.doGetRequest(url); 24 | }, 25 | fetchTestData(request = {}) { 26 | let url = 'api/test/fetch'; 27 | return restApi.doGetRequest(url); 28 | }, 29 | updateTestData(request = {}) { 30 | let url = 'api/test/update?newData=' + request.newData; 31 | return restApi.doPostRequest(url); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/front-vue/src/api/service/index.js: -------------------------------------------------------------------------------- 1 | import authService from './authService' 2 | 3 | export default { 4 | authService 5 | } 6 | -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/front-vue/src/assets/common.css: -------------------------------------------------------------------------------- 1 | html, body { 2 | height: 100%; 3 | } 4 | 5 | body { 6 | margin: 0px; 7 | } 8 | 9 | #app { 10 | height: 100%; 11 | } 12 | 13 | .user-info .user-logo { 14 | background: url("../assets/img/dog.jpg"); 15 | } 16 | 17 | .login-wrapper .login-fail-img { 18 | background: url("../assets/img/fail_login.jpg"); 19 | } -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/front-vue/src/assets/img/dog.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peipeihh/simple-oauth2/5cbbde9cdfd9fd2e75d1b3f087ab7cde02abed66/sample/demo-front-vue-spring-boot-web/front-vue/src/assets/img/dog.jpg -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/front-vue/src/assets/img/fail_login.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peipeihh/simple-oauth2/5cbbde9cdfd9fd2e75d1b3f087ab7cde02abed66/sample/demo-front-vue-spring-boot-web/front-vue/src/assets/img/fail_login.jpg -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/front-vue/src/components/Footer.vue: -------------------------------------------------------------------------------- 1 | 7 | 8 | 11 | 12 | -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/front-vue/src/components/SiderBar.vue: -------------------------------------------------------------------------------- 1 | 12 | 15 | 36 | -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/front-vue/src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Hello, This is simple demo how to connect with simple oauth. 7 | 8 | 9 | 10 | 11 |
12 | 13 |
14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/front-vue/src/main.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | 3 | import VueRouter from 'vue-router' 4 | Vue.use(VueRouter); 5 | 6 | import ElementUI from 'element-ui' 7 | import 'element-ui/lib/theme-chalk/index.css' 8 | Vue.use(ElementUI); 9 | 10 | import axios from 'axios' 11 | import router from './router' 12 | import store from './store' 13 | import jwtTokenUtil from "./utils/jwtTokenUtil"; 14 | 15 | 16 | /** 17 | * enable axios ajax call in the vue component 18 | * please see the usage example in the ./pages/pages/demo/Ajax.vue 19 | * @type {AxiosStatic} 20 | */ 21 | Vue.prototype.$http = axios; 22 | 23 | /** 24 | * enable the development mode 25 | * @type {boolean} 26 | */ 27 | Vue.config.devtools = process.env.NODE_ENV === 'development'; 28 | 29 | // http request 拦截器 30 | axios.interceptors.request.use( 31 | config => { 32 | let jwtToken = jwtTokenUtil.readAccess(); 33 | // 给http请求的header加上jwt-token 34 | config.headers['jwt-token'] = jwtToken; 35 | return config; 36 | }, 37 | error => { 38 | return Promise.reject(error); 39 | } 40 | ); 41 | 42 | /** 43 | * initialize the vue app with vuex store and vue router 44 | */ 45 | new Vue({ 46 | store, 47 | router, 48 | }).$mount('#app'); -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/front-vue/src/pages/Blank.vue: -------------------------------------------------------------------------------- 1 | 8 | 9 | 27 | 28 | -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/front-vue/src/pages/Layout.vue: -------------------------------------------------------------------------------- 1 | 15 | 16 | 45 | 46 | -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/front-vue/src/router/index.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import Router from 'vue-router' 3 | 4 | Vue.use(Router); 5 | 6 | 7 | import Layout from '../pages/Layout.vue' 8 | import BlankPage from '../pages/Blank.vue' 9 | import Login from '../pages/Login.vue' 10 | 11 | 12 | export default new Router({ 13 | mode: 'hash', // mode option: 1. hash (default), 2. history 14 | routes: [{ 15 | path: '', 16 | component: Layout, 17 | children: [{ 18 | path: '', 19 | name: 'blank', 20 | component: BlankPage 21 | }] 22 | }, { 23 | path: '/login', 24 | name: 'Login', 25 | component: Login, 26 | }], 27 | linkActiveClass: 'active' 28 | }) 29 | -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/front-vue/src/store/index.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import Vuex from 'vuex' 3 | import oauth from './model/oauth' 4 | Vue.use(Vuex); 5 | 6 | /** 7 | * detect current environment and set the debug configuration for vue store 8 | */ 9 | const debug_mode = process.env.NODE_ENV !== 'production'; 10 | 11 | /** 12 | * initialize the vuex store with actions/getters/modules 13 | */ 14 | export default new Vuex.Store({ 15 | modules: { 16 | oauth 17 | }, 18 | strict: debug_mode 19 | }) 20 | -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/front-vue/src/utils/jwtTokenUtil.js: -------------------------------------------------------------------------------- 1 | export default { 2 | 3 | saveAccess(token){ 4 | localStorage.setItem("access-token", token); 5 | }, 6 | 7 | readAccess(){ 8 | return localStorage.getItem("access-token"); 9 | }, 10 | 11 | saveRefresh(token){ 12 | localStorage.setItem("refresh-token", token); 13 | }, 14 | 15 | readRefresh(){ 16 | return localStorage.getItem("refresh-token"); 17 | }, 18 | 19 | clear(){ 20 | localStorage.removeItem("access-token"); 21 | localStorage.removeItem("refresh-token"); 22 | }, 23 | 24 | } -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/front-vue/src/utils/lastVisitUtil.js: -------------------------------------------------------------------------------- 1 | 2 | export default { 3 | 4 | save(url){ 5 | localStorage.setItem("last-visited", url); 6 | }, 7 | 8 | read(){ 9 | return localStorage.getItem("last-visited"); 10 | }, 11 | 12 | clear(){ 13 | localStorage.removeItem("last-visited"); 14 | } 15 | 16 | } -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/front-vue/src/vendor.js: -------------------------------------------------------------------------------- 1 | /** 2 | * list all 3rd party modules here, which will be packaged as vendor.js 3 | */ 4 | 5 | import Vue from 'vue' 6 | import ElementUI from 'element-ui' 7 | -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | oauth-sample 7 | com.pphh.demo 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | demo-front-vue-spring-boot-web 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-web 18 | 19 | 20 | com.pphh.demo 21 | oauth-spring-boot-websupport 22 | 23 | 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-maven-plugin 30 | ${springboot.version} 31 | 32 | 33 | 34 | repackage 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/src/main/java/com/pphh/oauth/sample/Application.java: -------------------------------------------------------------------------------- 1 | package com.pphh.oauth.sample; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | * Please add description here. 8 | * 9 | * @author huangyinhuang 10 | * @date 8/2/2018 11 | */ 12 | @SpringBootApplication 13 | public class Application { 14 | 15 | public static void main(String[] args) { 16 | SpringApplication.run(Application.class, args); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/src/main/java/com/pphh/oauth/sample/AutoConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.pphh.oauth.sample; 2 | 3 | import com.pphh.oauth.client.webcontroller.OAuthClientController; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | /** 8 | * Please add description here. 9 | * 10 | * @author huangyinhuang 11 | * @date 8/2/2018 12 | */ 13 | @Configuration 14 | public class AutoConfiguration { 15 | 16 | @Bean 17 | public OAuthClientController oauthLoginController() { 18 | return new OAuthClientController(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/src/main/java/com/pphh/oauth/sample/TestController.java: -------------------------------------------------------------------------------- 1 | package com.pphh.oauth.sample; 2 | 3 | import com.pphh.oauth.core.response.MessageType; 4 | import com.pphh.oauth.core.response.Response; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RequestMethod; 7 | import org.springframework.web.bind.annotation.RequestParam; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | /** 11 | * Please add description here. 12 | * 13 | * @author huangyinhuang 14 | * @date 8/2/2018 15 | */ 16 | @RestController 17 | @RequestMapping("/api/test") 18 | public class TestController { 19 | 20 | @RequestMapping(value = "/fetch", method = RequestMethod.GET) 21 | public Response getTest() { 22 | return Response.mark(MessageType.SUCCESS, "test"); 23 | } 24 | 25 | @RequestMapping(value = "/update", method = RequestMethod.POST) 26 | public Response updateTest(@RequestParam(value = "newData") String newData) { 27 | return Response.mark(MessageType.SUCCESS, newData); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /sample/demo-front-vue-spring-boot-web/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # server configuration 2 | server.port = 9007 3 | 4 | # oauth spring client settings 5 | oauth.server.url = http://localhost 6 | 7 | # oauth spring filter settings 8 | oauth.spring.filter.type = all-check-by-skip 9 | oauth.spring.filter.token.store.type = header 10 | oauth.spring.filter.token.name = jwt-token 11 | oauth.spring.filter.special.urls = GET&.* 12 | 13 | # oauth spring support settings 14 | oauth.client.id = demo 15 | oauth.client.callback = http://localhost:9006/#/login 16 | oauth.client.authorization = Basic ZGVtbzo1MGROOTI= -------------------------------------------------------------------------------- /sample/demo-front-vue/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "es2015", 4 | "stage-2" 5 | ], 6 | "plugins": [ 7 | "transform-runtime", 8 | "transform-vue-jsx" 9 | ], 10 | "comments": true 11 | } 12 | -------------------------------------------------------------------------------- /sample/demo-front-vue/.postcssrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | "plugins": { 3 | "autoprefixer": {} 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /sample/demo-front-vue/README.md: -------------------------------------------------------------------------------- 1 | 2 | ## 1. 简介 3 | 4 | 这是一个演示项目,使用前端vue框架(vue 2.0 + vue.router + vuex),对接Simple OAuth所提供的oauth 2.0 authorization grant授权方式,实现用户登录功能。 5 | 6 | ## 2. 项目的结构和构建 7 | 8 | ``` 9 | - README.md 使用说明文档 10 | - package.json NPM构建配置文件 11 | - webpack.config.js 前端webpack打包配置 12 | + src 13 | - index.html 前端应用主页面 14 | - main.js 前端应用入口 15 | + api 对后端API的接口调用 16 | + assets 静态资源文件 17 | + components VUE组件 18 | + pages 页面组件 19 | + router 前端路由 20 | + store 数据模型层 21 | ``` 22 | 23 | #### 2.1 安装node并配置npm源 24 | 25 | 登录node官方网站安装node 3.10.8+。 26 | 27 | 配置npm源为淘宝源 28 | 29 | ``` 30 | npm set registry "https://registry.npm.taobao.org/" 31 | ``` 32 | 33 | 配置后可以通过npm config list查看。 34 | 35 | #### 2.2 前端项目构建运行命令 36 | 37 | 构建文件:./package.json 38 | 39 | 下载依赖包:npm install,执行成功后将会把express依赖包下载到/node_modules目录。 40 | 41 | 构建命令:npm run build,构建成功后将会生成./dist目录。 42 | 43 | 运行命令:npm run dev,运行成功后可以通过[http://localhost:9002](http://localhost:9002)访问应用 44 | 45 | 注:应用的启动端口在./webpack.config.js文件中devServer.port选项所配置。 46 | 47 | ## 3. 演示 48 | 49 | 1. 准备工作 50 | - 根据oauth-server项目的README文档,启动simple oauth后端web服务。 51 | - 根据oauth-front项目的README文档,启动simple oauth前端web服务。 52 | - 若以项目缺省配置,simple oauth前端和后端分别启动在如下两个地址, 53 | ``` 54 | 前端服务 http://localhost 55 | 后端服务 http://localhost:8090 56 | ``` 57 | - 本演示项目将根据上面的两个地址进行授权登录跳转。 58 | 59 | 2. 注册应用 60 | - 请登录oauth前端页面,应该能看到auth server已经注册一个缺省的demo应用。 61 | * 应用查看列表:[http://localhost/#/dev/myclient](http://localhost/#/dev/myclient) 62 | 63 | - 若没有发现demo应用,可以手动注册应用, 64 | * client id = demo 65 | * 重定向返回地址 = .* (其含义为simple oauth接受demo client指定的任何返回地址) 66 | * 注册成功后,更新./webpack.config.js中下面的配置, 67 | ``` 68 | 'authorization': 'Basic ZGVtbzo1MGROOTI=', 69 | ``` 70 | 71 | 3. 启动当前演示项目 72 | - 执行前端应用运行命令:npm run dev 73 | - 打开浏览器,访问前端应用地址:http://localhost:9002 74 | - 点击登录按钮,将跳转到simple oauth的授权界面:http://localhost/#/authorize,点击同意按钮。 75 | * 注:若simple oauth没有登录,则需要先登录,再点击同意授权按钮,登录账号缺省为admin/admin,详情请查看simple auth项目readme文件。 76 | - 若一切正常,simple oauth将跳转回当前前端演示应用地址,并完成登录,显示登录账号。 77 | -------------------------------------------------------------------------------- /sample/demo-front-vue/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "front-vue", 3 | "version": "1.0.0", 4 | "description": "this is a simple demo of oauth.", 5 | "main": "webpack.config.js", 6 | "scripts": { 7 | "dev": "webpack-dev-server --inline --hot --env.dev", 8 | "build": "webpack -p --progress --hide-modules" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git@gitee.com:pphh/simple-oauth2.git" 13 | }, 14 | "keywords": [ 15 | "auth 2.0" 16 | ], 17 | "author": "pphh", 18 | "license": "Apache License 2.0", 19 | "dependencies": { 20 | "axios": "^0.16.2", 21 | "echarts": "^3.5.0", 22 | "element-ui": "^2.0.9", 23 | "jwt-decode": "^2.1.0", 24 | "vue": "^2.5.2", 25 | "vue-router": "^2.0.0", 26 | "vuex": "^2.3.1" 27 | }, 28 | "devDependencies": { 29 | "babel-core": "^6.0.0", 30 | "babel-loader": "^6.0.0", 31 | "babel-plugin-component": "^0.9.1", 32 | "babel-preset-env": "^1.6.1", 33 | "babel-preset-es2015": "^6.24.1", 34 | "babel-preset-stage-2": "^6.24.1", 35 | "babel-preset-vue-app": "^1.2.0", 36 | "cross-env": "^1.0.6", 37 | "css-loader": "^0.23.1", 38 | "file-loader": "^0.8.5", 39 | "html-webpack-plugin": "^2.24.1", 40 | "jsonwebtoken": "^7.3.0", 41 | "postcss-loader": "^1.3.3", 42 | "request": "^2.79.0", 43 | "style-loader": "^0.13.1", 44 | "uglifyjs-webpack-plugin": "^0.4.6", 45 | "url-loader": "^0.5.8", 46 | "vue-loader": "^9.8.0", 47 | "vue-style-loader": "^2.0.0", 48 | "webpack": "^2.4.5", 49 | "webpack-dev-server": "^2.4.5" 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /sample/demo-front-vue/src/api/index.js: -------------------------------------------------------------------------------- 1 | import axios from 'axios' 2 | import service from './service' 3 | 4 | axios.defaults.timeout = 60000; 5 | axios.defaults.headers.common['Content-Type'] = 'application/json'; 6 | axios.defaults.headers.post['Content-Type'] = 'application/json'; 7 | axios.defaults.headers.put['Content-Type'] = 'application/json'; 8 | 9 | export const api = service; -------------------------------------------------------------------------------- /sample/demo-front-vue/src/api/restApi.js: -------------------------------------------------------------------------------- 1 | import axios from 'axios' 2 | 3 | export default { 4 | 5 | doGetRequest(url){ 6 | return axios.get(url) 7 | .then((response) => Promise.resolve(response)) 8 | .catch((error) => Promise.reject(error)) 9 | }, 10 | doDeleteRequest(url){ 11 | return axios.delete(url) 12 | .then((response) => Promise.resolve(response)) 13 | .catch((error) => Promise.reject(error)) 14 | }, 15 | doPutRequest(url, data){ 16 | if (typeof(data) == "object") { 17 | data = JSON.stringify(data); 18 | } 19 | return axios.put(url, data) 20 | .then((response) => Promise.resolve(response)) 21 | .catch((error) => Promise.reject(error)) 22 | }, 23 | doPostRequest(url, data){ 24 | if (typeof(data) == "object") { 25 | data = JSON.stringify(data); 26 | } 27 | return axios.post(url, data) 28 | .then((response) => Promise.resolve(response)) 29 | .catch((error) => Promise.reject(error)) 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /sample/demo-front-vue/src/api/service/authService.js: -------------------------------------------------------------------------------- 1 | import restApi from '../restApi' 2 | 3 | export default { 4 | fetchToken(request = {}) { 5 | let url = 'api/oauth2/accessToken?code=' + request.code; 6 | return restApi.doGetRequest(url); 7 | }, 8 | refreshToken(request = {}) { 9 | let url = 'api/oauth2/refreshToken?refresh_token=' + request.refresh_token; 10 | return restApi.doGetRequest(url); 11 | }, 12 | revokeToken(request = {}) { 13 | let url = 'api/oauth2/revokeToken?token=' + request.token; 14 | return restApi.doGetRequest(url); 15 | }, 16 | fetchLoginUrl(request = {}) { 17 | let url = null; 18 | if (request != null && request.callback != null) { 19 | url = 'api/oauth2/redirectUrl?callback=' + encodeURIComponent(request.callback); 20 | } else { 21 | url = 'api/oauth2/redirectUrl'; 22 | } 23 | return restApi.doGetRequest(url); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /sample/demo-front-vue/src/api/service/index.js: -------------------------------------------------------------------------------- 1 | import authService from './authService' 2 | 3 | export default { 4 | authService 5 | } 6 | -------------------------------------------------------------------------------- /sample/demo-front-vue/src/assets/common.css: -------------------------------------------------------------------------------- 1 | html, body { 2 | height: 100%; 3 | } 4 | 5 | body { 6 | margin: 0px; 7 | } 8 | 9 | #app { 10 | height: 100%; 11 | } 12 | 13 | .user-info .user-logo { 14 | background: url("../assets/img/dog.jpg"); 15 | } 16 | 17 | .login-wrapper .login-fail-img { 18 | background: url("../assets/img/fail_login.jpg"); 19 | } -------------------------------------------------------------------------------- /sample/demo-front-vue/src/assets/img/dog.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peipeihh/simple-oauth2/5cbbde9cdfd9fd2e75d1b3f087ab7cde02abed66/sample/demo-front-vue/src/assets/img/dog.jpg -------------------------------------------------------------------------------- /sample/demo-front-vue/src/assets/img/fail_login.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peipeihh/simple-oauth2/5cbbde9cdfd9fd2e75d1b3f087ab7cde02abed66/sample/demo-front-vue/src/assets/img/fail_login.jpg -------------------------------------------------------------------------------- /sample/demo-front-vue/src/components/Footer.vue: -------------------------------------------------------------------------------- 1 | 7 | 8 | 11 | 12 | -------------------------------------------------------------------------------- /sample/demo-front-vue/src/components/SiderBar.vue: -------------------------------------------------------------------------------- 1 | 12 | 15 | 36 | -------------------------------------------------------------------------------- /sample/demo-front-vue/src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Hello, This is simple demo how to connect with simple oauth. 7 | 8 | 9 | 10 | 11 |
12 | 13 |
14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /sample/demo-front-vue/src/main.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | 3 | import VueRouter from 'vue-router' 4 | Vue.use(VueRouter); 5 | 6 | import ElementUI from 'element-ui' 7 | import 'element-ui/lib/theme-chalk/index.css' 8 | Vue.use(ElementUI); 9 | 10 | import axios from 'axios' 11 | import router from './router' 12 | import store from './store' 13 | import jwtTokenUtil from "./utils/jwtTokenUtil"; 14 | 15 | 16 | /** 17 | * enable axios ajax call in the vue component 18 | * please see the usage example in the ./pages/pages/demo/Ajax.vue 19 | * @type {AxiosStatic} 20 | */ 21 | Vue.prototype.$http = axios; 22 | 23 | /** 24 | * enable the development mode 25 | * @type {boolean} 26 | */ 27 | Vue.config.devtools = process.env.NODE_ENV === 'development'; 28 | 29 | // http request 拦截器 30 | axios.interceptors.request.use( 31 | config => { 32 | let jwtToken = jwtTokenUtil.readAccess(); 33 | // 给http请求的header加上jwt-token 34 | config.headers['jwt-token'] = jwtToken; 35 | return config; 36 | }, 37 | error => { 38 | return Promise.reject(error); 39 | } 40 | ); 41 | 42 | /** 43 | * initialize the vue app with vuex store and vue router 44 | */ 45 | new Vue({ 46 | store, 47 | router, 48 | }).$mount('#app'); -------------------------------------------------------------------------------- /sample/demo-front-vue/src/pages/Blank.vue: -------------------------------------------------------------------------------- 1 | 4 | 5 | 8 | 9 | -------------------------------------------------------------------------------- /sample/demo-front-vue/src/pages/Layout.vue: -------------------------------------------------------------------------------- 1 | 15 | 16 | 45 | 46 | -------------------------------------------------------------------------------- /sample/demo-front-vue/src/router/index.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import Router from 'vue-router' 3 | 4 | Vue.use(Router); 5 | 6 | 7 | import Layout from '../pages/Layout.vue' 8 | import BlankPage from '../pages/Blank.vue' 9 | import Login from '../pages/Login.vue' 10 | 11 | 12 | export default new Router({ 13 | mode: 'hash', // mode option: 1. hash (default), 2. history 14 | routes: [{ 15 | path: '', 16 | component: Layout, 17 | children: [{ 18 | path: '', 19 | name: 'blank', 20 | component: BlankPage 21 | }] 22 | }, { 23 | path: '/login', 24 | name: 'Login', 25 | component: Login, 26 | }], 27 | linkActiveClass: 'active' 28 | }) 29 | -------------------------------------------------------------------------------- /sample/demo-front-vue/src/store/index.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import Vuex from 'vuex' 3 | import oauth from './model/oauth' 4 | Vue.use(Vuex); 5 | 6 | /** 7 | * detect current environment and set the debug configuration for vue store 8 | */ 9 | const debug_mode = process.env.NODE_ENV !== 'production'; 10 | 11 | /** 12 | * initialize the vuex store with actions/getters/modules 13 | */ 14 | export default new Vuex.Store({ 15 | modules: { 16 | oauth 17 | }, 18 | strict: debug_mode 19 | }) 20 | -------------------------------------------------------------------------------- /sample/demo-front-vue/src/utils/jwtTokenUtil.js: -------------------------------------------------------------------------------- 1 | export default { 2 | 3 | saveAccess(token){ 4 | localStorage.setItem("access-token", token); 5 | }, 6 | 7 | readAccess(){ 8 | return localStorage.getItem("access-token"); 9 | }, 10 | 11 | saveRefresh(token){ 12 | localStorage.setItem("refresh-token", token); 13 | }, 14 | 15 | readRefresh(){ 16 | return localStorage.getItem("refresh-token"); 17 | }, 18 | 19 | clear(){ 20 | localStorage.removeItem("access-token"); 21 | localStorage.removeItem("refresh-token"); 22 | }, 23 | 24 | } -------------------------------------------------------------------------------- /sample/demo-front-vue/src/utils/lastVisitUtil.js: -------------------------------------------------------------------------------- 1 | 2 | export default { 3 | 4 | save(url){ 5 | localStorage.setItem("last-visited", url); 6 | }, 7 | 8 | read(){ 9 | return localStorage.getItem("last-visited"); 10 | }, 11 | 12 | clear(){ 13 | localStorage.removeItem("last-visited"); 14 | } 15 | 16 | } -------------------------------------------------------------------------------- /sample/demo-front-vue/src/vendor.js: -------------------------------------------------------------------------------- 1 | /** 2 | * list all 3rd party modules here, which will be packaged as vendor.js 3 | */ 4 | 5 | import Vue from 'vue' 6 | import ElementUI from 'element-ui' 7 | -------------------------------------------------------------------------------- /sample/demo-spring-boot-web/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | oauth-sample 7 | com.pphh.demo 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | demo-spring-boot-web 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-web 18 | 19 | 20 | com.pphh.demo 21 | oauth-spring-boot-autoconfigure 22 | 23 | 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-maven-plugin 30 | ${springboot.version} 31 | 32 | 33 | 34 | repackage 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /sample/demo-spring-boot-web/src/main/java/com/pphh/oauth/sample/AppProperties.java: -------------------------------------------------------------------------------- 1 | package com.pphh.oauth.sample; 2 | 3 | /** 4 | * Please add description here. 5 | * 6 | * @author huangyinhuang 7 | * @date 8/1/2018 8 | */ 9 | public class AppProperties { 10 | 11 | public static String cookieDomain = "localhost"; 12 | public static String cookiePath = "/"; 13 | public static boolean cookieSecure = false; 14 | public static int cookieExpiry = 3600; 15 | 16 | } 17 | -------------------------------------------------------------------------------- /sample/demo-spring-boot-web/src/main/java/com/pphh/oauth/sample/UserCredential.java: -------------------------------------------------------------------------------- 1 | package com.pphh.oauth.sample; 2 | 3 | /** 4 | * Please add description here. 5 | * 6 | * @author huangyinhuang 7 | * @date 8/1/2018 8 | */ 9 | public class UserCredential { 10 | 11 | String userName; 12 | String userPwd; 13 | 14 | public String getUserName() { 15 | return userName; 16 | } 17 | 18 | public void setUserName(String userName) { 19 | this.userName = userName; 20 | } 21 | 22 | public String getUserPwd() { 23 | return userPwd; 24 | } 25 | 26 | public void setUserPwd(String userPwd) { 27 | this.userPwd = userPwd; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /sample/demo-spring-boot-web/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port = 9005 2 | 3 | # oauth client settings 4 | oauth.client.id = demo 5 | oauth.client.secret = 50dN92 6 | 7 | # oauth spring filter settings 8 | oauth.server.url = http://localhost:8090 9 | oauth.spring.filter.token.store.type = cookie 10 | oauth.spring.filter.token.name = oauth-token 11 | oauth.spring.filter.skip.url = /login.html,/logout.html,/index.html,/api/login,/api/logout -------------------------------------------------------------------------------- /sample/demo-spring-boot-web/src/main/resources/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 演示OAuth 2.0 Resource Owner Password Grant 6 | 7 | 8 |

演示OAuth 2.0 Resource Owner Password Grant

9 |

请先到登录页面,尝试登录,然后访问后端api,获取用户登录信息

10 | 11 | 12 | 13 | 14 | 15 | 16 | 19 | 20 | 21 |
请点击访问后端API,获取用户信息 17 | 18 |
22 | 23 |
    24 |
  1. 回到首页
  2. 25 |
  3. 登录页面
  4. 26 |
  5. 注销登录页面
  6. 27 |
28 | 29 | 30 | 31 | 52 | -------------------------------------------------------------------------------- /sample/demo-spring-boot-web/src/main/resources/static/login.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 演示OAuth 2.0 Resource Owner Password Grant 6 | 7 | 8 |

演示-登录

9 |

请输入用户名和密码,并点击登录按钮

10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 25 | 26 | 27 |
User:
Password:
23 | 24 |
28 | 29 |
    30 |
  1. 回到首页
  2. 31 |
  3. 登录页面
  4. 32 |
  5. 注销登录页面
  6. 33 |
34 | 35 | 36 | 37 | 38 | 74 | 75 | -------------------------------------------------------------------------------- /sample/demo-spring-boot-web/src/main/resources/static/logout.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 演示OAuth 2.0 Resource Owner Password Grant 6 | 7 | 8 |

演示 - Logout

9 |

请点击登出按钮,这会清除登录的cookie信息。

10 | 11 | 12 | 13 | 14 | 15 | 18 | 19 | 20 |
请点击登出按钮 16 | 17 |
21 | 22 |
    23 |
  1. 回到首页
  2. 24 |
  3. 登录页面
  4. 25 |
  5. 注销登录页面
  6. 26 |
27 | 28 | 29 | 30 | 57 | -------------------------------------------------------------------------------- /sample/demo-web-service/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | oauth-sample 7 | com.pphh.demo 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | demo-web-service 13 | pom 14 | 15 | resource-client 16 | resource-server 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /sample/demo-web-service/resource-client/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | demo-web-service 7 | com.pphh.demo 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | resource-client 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-web 18 | 19 | 20 | com.pphh.demo 21 | oauth-spring-boot-autoconfigure 22 | 23 | 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-maven-plugin 30 | ${springboot.version} 31 | 32 | 33 | 34 | repackage 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /sample/demo-web-service/resource-client/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port = 9004 2 | 3 | # oauth spring filter settings - remote api 4 | oauth.server.url = http://localhost:8090 5 | 6 | # oauth client settings 7 | oauth.client.id = demo 8 | oauth.client.secret = 50dN92 9 | 10 | # resource server 11 | resource.server.url = http://localhost:9003/hello -------------------------------------------------------------------------------- /sample/demo-web-service/resource-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | demo-web-service 7 | com.pphh.demo 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | resource-server 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-web 18 | 19 | 20 | com.pphh.demo 21 | oauth-spring-boot-autoconfigure 22 | 23 | 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-maven-plugin 30 | ${springboot.version} 31 | 32 | 33 | 34 | repackage 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /sample/demo-web-service/resource-server/src/main/java/com/pphh/oauth/sample/ResourceServer.java: -------------------------------------------------------------------------------- 1 | package com.pphh.oauth.sample; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.http.HttpStatus; 6 | import org.springframework.http.ResponseEntity; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | /** 11 | * Please add description here. 12 | * 13 | * @author huangyinhuang 14 | * @date 8/2/2018 15 | */ 16 | @SpringBootApplication 17 | @RestController 18 | public class ResourceServer { 19 | 20 | public static void main(String[] args) { 21 | SpringApplication.run(ResourceServer.class, args); 22 | } 23 | 24 | @GetMapping("/hello") 25 | public ResponseEntity greet() { 26 | return new ResponseEntity<>("hello, I am protected by oauth filter.", HttpStatus.OK); 27 | } 28 | 29 | @GetMapping("/login") 30 | public ResponseEntity login() { 31 | return new ResponseEntity<>("hello, this is login api.", HttpStatus.OK); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /sample/demo-web-service/resource-server/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port = 9003 2 | 3 | # oauth spring filter settings - remote api 4 | oauth.server.url = http://localhost:8090 5 | 6 | # oauth spring filter settings 7 | oauth.spring.filter.type = all-check-by-skip 8 | oauth.spring.filter.token.store.type = header 9 | oauth.spring.filter.token.name = resource-token 10 | oauth.spring.filter.special.urls = /login 11 | --------------------------------------------------------------------------------