Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-microservice-exam
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
汪想
spring-microservice-exam
Commits
8a2ca63e
Commit
8a2ca63e
authored
Apr 08, 2019
by
tangyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化
parent
06a9c8fd
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
54 additions
and
8 deletions
+54
-8
CommonConstant.java
...om/github/tangyi/common/core/constant/CommonConstant.java
+5
-0
auth-service.yml
config-service/src/main/resources/config/auth-service.yml
+1
-0
exam-service.yml
config-service/src/main/resources/config/exam-service.yml
+1
-0
user-service.yml
config-service/src/main/resources/config/user-service.yml
+1
-0
AuthenticationController.java
...thub/tangyi/auth/controller/AuthenticationController.java
+5
-4
UserDetailsServiceImpl.java
...om/github/tangyi/auth/service/UserDetailsServiceImpl.java
+15
-2
UserService.java
...main/java/com/github/tangyi/user/service/UserService.java
+1
-2
UserServiceClient.java
...a/com/github/tangyi/user/api/feign/UserServiceClient.java
+12
-0
UserServiceClientFallbackImpl.java
...ser/api/feign/fallback/UserServiceClientFallbackImpl.java
+13
-0
No files found.
common/common-core/src/main/java/com/github/tangyi/common/core/constant/CommonConstant.java
View file @
8a2ca63e
...
@@ -138,5 +138,10 @@ public class CommonConstant {
...
@@ -138,5 +138,10 @@ public class CommonConstant {
*/
*/
public
static
final
String
DEFAULT_CODE_KEY
=
"DEFAULT_CODE_KEY"
;
public
static
final
String
DEFAULT_CODE_KEY
=
"DEFAULT_CODE_KEY"
;
/**
* Bearer
*/
public
static
final
String
AUTHORIZATION_BEARER
=
"Bearer "
;
}
}
config-service/src/main/resources/config/auth-service.yml
View file @
8a2ca63e
...
@@ -77,6 +77,7 @@ ignore:
...
@@ -77,6 +77,7 @@ ignore:
-
/actuator/**
-
/actuator/**
-
/hystrix.sender
-
/hystrix.sender
-
/v1/user/findUserByUsername/**
-
/v1/user/findUserByUsername/**
-
/v1/menu/findMenuByRole/**
-
/v1/code/**
-
/v1/code/**
-
/v1/attachment/download
-
/v1/attachment/download
-
/v1/log/**
-
/v1/log/**
...
...
config-service/src/main/resources/config/exam-service.yml
View file @
8a2ca63e
...
@@ -91,6 +91,7 @@ ignore:
...
@@ -91,6 +91,7 @@ ignore:
-
/actuator/**
-
/actuator/**
-
/hystrix.sender
-
/hystrix.sender
-
/v1/user/findUserByUsername/**
-
/v1/user/findUserByUsername/**
-
/v1/menu/findMenuByRole/**
-
/v1/code/**
-
/v1/code/**
-
/v1/attachment/download
-
/v1/attachment/download
-
/v1/log/**
-
/v1/log/**
...
...
config-service/src/main/resources/config/user-service.yml
View file @
8a2ca63e
...
@@ -109,6 +109,7 @@ ignore:
...
@@ -109,6 +109,7 @@ ignore:
-
/actuator/**
-
/actuator/**
-
/hystrix.sender
-
/hystrix.sender
-
/v1/user/findUserByUsername/**
-
/v1/user/findUserByUsername/**
-
/v1/menu/findMenuByRole/**
-
/v1/user/register
-
/v1/user/register
-
/v1/code/**
-
/v1/code/**
-
/v1/attachment/download
-
/v1/attachment/download
...
...
service-api-impl/auth-service/src/main/java/com/github/tangyi/auth/controller/AuthenticationController.java
View file @
8a2ca63e
package
com
.
github
.
tangyi
.
auth
.
controller
;
package
com
.
github
.
tangyi
.
auth
.
controller
;
import
com.github.tangyi.common.
security.constant.Security
Constant
;
import
com.github.tangyi.common.
core.constant.Common
Constant
;
import
com.github.tangyi.common.core.model.ResponseBean
;
import
com.github.tangyi.common.core.model.ResponseBean
;
import
com.github.tangyi.common.core.web.BaseController
;
import
com.github.tangyi.common.core.web.BaseController
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.cache.annotation.CacheEvict
;
import
org.springframework.security.core.Authentication
;
import
org.springframework.security.core.Authentication
;
import
org.springframework.security.oauth2.provider.token.ConsumerTokenServices
;
import
org.springframework.security.oauth2.provider.token.ConsumerTokenServices
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestHeader
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.RestController
;
...
@@ -44,8 +44,9 @@ public class AuthenticationController extends BaseController {
...
@@ -44,8 +44,9 @@ public class AuthenticationController extends BaseController {
* @return ReturnT
* @return ReturnT
*/
*/
@PostMapping
(
"/removeToken"
)
@PostMapping
(
"/removeToken"
)
@CacheEvict
(
value
=
SecurityConstant
.
TOKEN_USER_DETAIL
,
key
=
"#accesstoken"
)
public
ResponseBean
<
Boolean
>
removeToken
(
@RequestHeader
(
"Authorization"
)
String
accesstoken
)
{
public
ResponseBean
<
Boolean
>
removeToken
(
String
accesstoken
)
{
if
(
accesstoken
.
startsWith
(
CommonConstant
.
AUTHORIZATION_BEARER
))
accesstoken
=
accesstoken
.
split
(
CommonConstant
.
AUTHORIZATION_BEARER
)[
1
];
return
new
ResponseBean
<>(
consumerTokenServices
.
revokeToken
(
accesstoken
));
return
new
ResponseBean
<>(
consumerTokenServices
.
revokeToken
(
accesstoken
));
}
}
}
}
service-api-impl/auth-service/src/main/java/com/github/tangyi/auth/service/UserDetailsServiceImpl.java
View file @
8a2ca63e
...
@@ -5,6 +5,7 @@ import com.github.tangyi.common.core.vo.UserVo;
...
@@ -5,6 +5,7 @@ import com.github.tangyi.common.core.vo.UserVo;
import
com.github.tangyi.common.security.core.GrantedAuthorityImpl
;
import
com.github.tangyi.common.security.core.GrantedAuthorityImpl
;
import
com.github.tangyi.common.security.core.UserDetailsImpl
;
import
com.github.tangyi.common.security.core.UserDetailsImpl
;
import
com.github.tangyi.user.api.feign.UserServiceClient
;
import
com.github.tangyi.user.api.feign.UserServiceClient
;
import
com.github.tangyi.user.api.module.Menu
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
...
@@ -16,6 +17,7 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
...
@@ -16,6 +17,7 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
java.util.HashSet
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Set
;
import
java.util.Set
;
/**
/**
...
@@ -62,11 +64,22 @@ public class UserDetailsServiceImpl implements UserDetailsService {
...
@@ -62,11 +64,22 @@ public class UserDetailsServiceImpl implements UserDetailsService {
* @date 2019/03/17 14:41
* @date 2019/03/17 14:41
*/
*/
private
Set
<
GrantedAuthority
>
getAuthority
(
UserVo
userVo
)
{
private
Set
<
GrantedAuthority
>
getAuthority
(
UserVo
userVo
)
{
// 权限集合
Set
<
GrantedAuthority
>
authorities
=
new
HashSet
<>();
Set
<
GrantedAuthority
>
authorities
=
new
HashSet
<>();
if
(
CollectionUtils
.
isNotEmpty
(
userVo
.
getRoleList
()))
{
// 角色
for
(
Role
role
:
userVo
.
getRoleList
())
{
List
<
Role
>
roleList
=
userVo
.
getRoleList
();
if
(
CollectionUtils
.
isNotEmpty
(
roleList
))
{
for
(
Role
role
:
roleList
)
{
// 权限如果前缀是ROLE_,security就会认为这是个角色信息,而不是权限,例如ROLE_ADMIN就是ADMIN角色,MENU:ADD就是MENU:ADD权限
// 权限如果前缀是ROLE_,security就会认为这是个角色信息,而不是权限,例如ROLE_ADMIN就是ADMIN角色,MENU:ADD就是MENU:ADD权限
authorities
.
add
(
new
GrantedAuthorityImpl
(
role
.
getRoleCode
().
toUpperCase
()));
authorities
.
add
(
new
GrantedAuthorityImpl
(
role
.
getRoleCode
().
toUpperCase
()));
// 根据角色查找菜单权限
List
<
Menu
>
menuList
=
userServiceClient
.
findMenuByRole
(
role
.
getRoleCode
());
if
(
CollectionUtils
.
isNotEmpty
(
menuList
))
{
for
(
Menu
menu
:
menuList
)
{
// 菜单权限
authorities
.
add
(
new
GrantedAuthorityImpl
(
menu
.
getPermission
()));
}
}
}
}
}
}
return
authorities
;
return
authorities
;
...
...
service-api-impl/user-service/src/main/java/com/github/tangyi/user/service/UserService.java
View file @
8a2ca63e
...
@@ -173,8 +173,7 @@ public class UserService extends CrudService<UserMapper, User> {
...
@@ -173,8 +173,7 @@ public class UserService extends CrudService<UserMapper, User> {
*/
*/
@Cacheable
(
value
=
"user"
,
key
=
"#username"
)
@Cacheable
(
value
=
"user"
,
key
=
"#username"
)
public
UserVo
selectUserVoByUsername
(
String
username
)
{
public
UserVo
selectUserVoByUsername
(
String
username
)
{
UserVo
userVo
=
userMapper
.
selectUserVoByUsername
(
username
);
return
userMapper
.
selectUserVoByUsername
(
username
);
return
userVo
;
}
}
/**
/**
...
...
service-api/user-api/src/main/java/com/github/tangyi/user/api/feign/UserServiceClient.java
View file @
8a2ca63e
...
@@ -9,6 +9,7 @@ import com.github.tangyi.common.core.vo.UserVo;
...
@@ -9,6 +9,7 @@ import com.github.tangyi.common.core.vo.UserVo;
import
com.github.tangyi.common.feign.config.CustomFeignConfig
;
import
com.github.tangyi.common.feign.config.CustomFeignConfig
;
import
com.github.tangyi.user.api.dto.UserInfoDto
;
import
com.github.tangyi.user.api.dto.UserInfoDto
;
import
com.github.tangyi.user.api.feign.factory.UserServiceClientFallbackFactory
;
import
com.github.tangyi.user.api.feign.factory.UserServiceClientFallbackFactory
;
import
com.github.tangyi.user.api.module.Menu
;
import
org.springframework.cloud.openfeign.FeignClient
;
import
org.springframework.cloud.openfeign.FeignClient
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.bind.annotation.*
;
...
@@ -92,4 +93,15 @@ public interface UserServiceClient {
...
@@ -92,4 +93,15 @@ public interface UserServiceClient {
*/
*/
@PostMapping
(
"/v1/log"
)
@PostMapping
(
"/v1/log"
)
ResponseBean
<
Boolean
>
saveLog
(
@RequestBody
Log
log
);
ResponseBean
<
Boolean
>
saveLog
(
@RequestBody
Log
log
);
/**
* 根据角色查找菜单
*
* @param role 角色
* @return List
* @author tangyi
* @date 2019/04/08 20:42
*/
@GetMapping
(
"/v1/menu/findMenuByRole/{role}"
)
List
<
Menu
>
findMenuByRole
(
@PathVariable
(
"role"
)
String
role
);
}
}
service-api/user-api/src/main/java/com/github/tangyi/user/api/feign/fallback/UserServiceClientFallbackImpl.java
View file @
8a2ca63e
...
@@ -7,6 +7,7 @@ import com.github.tangyi.common.core.vo.DeptVo;
...
@@ -7,6 +7,7 @@ import com.github.tangyi.common.core.vo.DeptVo;
import
com.github.tangyi.common.core.vo.UserVo
;
import
com.github.tangyi.common.core.vo.UserVo
;
import
com.github.tangyi.user.api.dto.UserInfoDto
;
import
com.github.tangyi.user.api.dto.UserInfoDto
;
import
com.github.tangyi.user.api.feign.UserServiceClient
;
import
com.github.tangyi.user.api.feign.UserServiceClient
;
import
com.github.tangyi.user.api.module.Menu
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
...
@@ -111,6 +112,18 @@ public class UserServiceClientFallbackImpl implements UserServiceClient {
...
@@ -111,6 +112,18 @@ public class UserServiceClientFallbackImpl implements UserServiceClient {
return
null
;
return
null
;
}
}
/**
* 根据角色查找菜单
*
* @param role 角色
* @return List
*/
@Override
public
List
<
Menu
>
findMenuByRole
(
String
role
)
{
logger
.
error
(
"feign 获取角色菜单失败,{}"
,
throwable
);
return
new
ArrayList
<>();
}
public
Throwable
getThrowable
()
{
public
Throwable
getThrowable
()
{
return
throwable
;
return
throwable
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment