Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
学习是不可能停的
micro-service-skeleton
提交
1d2fac62
M
micro-service-skeleton
项目概览
学习是不可能停的
/
micro-service-skeleton
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
micro-service-skeleton
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
1d2fac62
编写于
12月 28, 2017
作者:
M
ManongJu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
auth2.0,把token存数据库,另外提供存redis
上级
0ff5fdaf
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
123 addition
and
22 deletion
+123
-22
auth-center/pom.xml
auth-center/pom.xml
+12
-3
auth-center/src/main/java/com/microservice/skeleton/auth/AuthCenterApplication.java
...com/microservice/skeleton/auth/AuthCenterApplication.java
+2
-1
auth-center/src/main/java/com/microservice/skeleton/auth/config/AuthorizationServerConfig.java
...rvice/skeleton/auth/config/AuthorizationServerConfig.java
+72
-0
auth-center/src/main/java/com/microservice/skeleton/auth/config/WebSecurityConfig.java
.../microservice/skeleton/auth/config/WebSecurityConfig.java
+17
-11
auth-center/src/main/java/com/microservice/skeleton/auth/service/impl/UserDetailsServiceImpl.java
...ce/skeleton/auth/service/impl/UserDetailsServiceImpl.java
+9
-3
auth-center/src/main/resources/application.yml
auth-center/src/main/resources/application.yml
+11
-4
未找到文件。
auth-center/pom.xml
浏览文件 @
1d2fac62
...
...
@@ -26,12 +26,12 @@
<artifactId>
spring-cloud-starter-eureka
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.
boot
</groupId>
<artifactId>
spring-
boot-starter-security
</artifactId>
<groupId>
org.springframework.
cloud
</groupId>
<artifactId>
spring-
cloud-starter-oauth2
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-
oauth2
</artifactId>
<artifactId>
spring-cloud-starter-
security
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
...
...
@@ -58,6 +58,15 @@
<groupId>
com.alibaba
</groupId>
<artifactId>
druid-spring-boot-starter
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-redis
</artifactId>
</dependency>
<dependency>
<groupId>
org.projectlombok
</groupId>
<artifactId>
lombok
</artifactId>
</dependency>
</dependencies>
<build>
...
...
auth-center/src/main/java/com/microservice/skeleton/auth/AuthCenterApplication.java
浏览文件 @
1d2fac62
...
...
@@ -3,9 +3,10 @@ package com.microservice.skeleton.auth;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.cloud.client.discovery.EnableDiscoveryClient
;
import
org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer
;
@SpringBootApplication
@EnableDiscoveryClient
//
@EnableDiscoveryClient
public
class
AuthCenterApplication
{
public
static
void
main
(
String
[]
args
)
{
...
...
auth-center/src/main/java/com/microservice/skeleton/auth/config/AuthorizationServerConfig.java
0 → 100644
浏览文件 @
1d2fac62
package
com.microservice.skeleton.auth.config
;
import
com.microservice.skeleton.auth.service.impl.UserDetailsServiceImpl
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.security.authentication.AuthenticationManager
;
import
org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer
;
import
org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter
;
import
org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer
;
import
org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer
;
import
org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer
;
import
org.springframework.security.oauth2.provider.ClientDetailsService
;
import
org.springframework.security.oauth2.provider.client.JdbcClientDetailsService
;
import
org.springframework.security.oauth2.provider.token.DefaultTokenServices
;
import
org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore
;
import
org.springframework.security.oauth2.provider.token.store.JdbcTokenStore
;
import
javax.sql.DataSource
;
import
java.util.concurrent.TimeUnit
;
/**
* Created by Mr.Yangxiufeng on 2017/12/28.
* Time:11:02
* ProjectName:Mirco-Service-Skeleton
*/
@Configuration
@EnableAuthorizationServer
public
class
AuthorizationServerConfig
extends
AuthorizationServerConfigurerAdapter
{
@Autowired
private
AuthenticationManager
authenticationManager
;
@Autowired
private
DataSource
dataSource
;
@Autowired
private
UserDetailsServiceImpl
userDetailsService
;
@Bean
public
JdbcTokenStore
jdbcTokenStore
(){
return
new
JdbcTokenStore
(
dataSource
);
}
@Override
public
void
configure
(
ClientDetailsServiceConfigurer
clients
)
throws
Exception
{
clients
.
withClientDetails
(
clientDetails
());
}
@Bean
public
ClientDetailsService
clientDetails
()
{
return
new
JdbcClientDetailsService
(
dataSource
);
}
@Override
public
void
configure
(
AuthorizationServerEndpointsConfigurer
endpoints
)
throws
Exception
{
endpoints
.
tokenStore
(
jdbcTokenStore
())
.
userDetailsService
(
userDetailsService
)
.
authenticationManager
(
authenticationManager
);
DefaultTokenServices
tokenServices
=
new
DefaultTokenServices
();
tokenServices
.
setTokenStore
(
endpoints
.
getTokenStore
());
tokenServices
.
setSupportRefreshToken
(
true
);
tokenServices
.
setClientDetailsService
(
endpoints
.
getClientDetailsService
());
tokenServices
.
setTokenEnhancer
(
endpoints
.
getTokenEnhancer
());
// tokenServices.setAccessTokenValiditySeconds( (int) TimeUnit.DAYS.toSeconds(30)); // token有效期自定义设置,默认12小时
endpoints
.
tokenServices
(
tokenServices
);
}
@Override
public
void
configure
(
AuthorizationServerSecurityConfigurer
security
)
throws
Exception
{
security
.
tokenKeyAccess
(
"permitAll()"
);
security
.
checkTokenAccess
(
"isAuthenticated()"
);
security
.
allowFormAuthenticationForClients
();
}
}
auth-center/src/main/java/com/microservice/skeleton/auth/config/WebSecurityConfig.java
浏览文件 @
1d2fac62
...
...
@@ -4,9 +4,10 @@ import com.microservice.skeleton.auth.service.impl.UserDetailsServiceImpl;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.security.authentication.AuthenticationManager
;
import
org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
;
import
org.springframework.security.config.annotation.web.builders.HttpSecurity
;
import
org.springframework.security.config.annotation.web.
configuration.Enable
WebSecurity
;
import
org.springframework.security.config.annotation.web.
builders.
WebSecurity
;
import
org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
;
import
org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
;
import
org.springframework.security.crypto.password.PasswordEncoder
;
...
...
@@ -17,7 +18,6 @@ import org.springframework.security.crypto.password.PasswordEncoder;
* ProjectName:Mirco-Service-Skeleton
*/
@Configuration
@EnableWebSecurity
public
class
WebSecurityConfig
extends
WebSecurityConfigurerAdapter
{
@Autowired
private
UserDetailsServiceImpl
userDetailsService
;
...
...
@@ -27,6 +27,11 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
return
new
BCryptPasswordEncoder
();
}
@Override
@Bean
public
AuthenticationManager
authenticationManagerBean
()
throws
Exception
{
return
super
.
authenticationManagerBean
();
}
@Override
protected
void
configure
(
AuthenticationManagerBuilder
auth
)
throws
Exception
{
auth
.
userDetailsService
(
userDetailsService
)
...
...
@@ -35,16 +40,17 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected
void
configure
(
HttpSecurity
http
)
throws
Exception
{
http
.
authorizeRequests
()
http
.
authorizeRequests
()
.
anyRequest
().
authenticated
()
.
and
()
.
formLogin
()
.
loginPage
(
"/login"
)
.
failureUrl
(
"/login?error"
)
.
permitAll
()
.
and
()
.
logout
()
.
permitAll
();
http
.
csrf
().
disable
().
httpBasic
(
);
.
formLogin
().
and
()
.
csrf
().
disable
(
)
.
httpBasic
();
}
@Override
public
void
configure
(
WebSecurity
web
)
throws
Exception
{
web
.
ignoring
().
antMatchers
(
"/favor.ioc"
);
}
}
auth-center/src/main/java/com/microservice/skeleton/auth/service/impl/UserDetailsServiceImpl.java
浏览文件 @
1d2fac62
...
...
@@ -19,17 +19,23 @@ import java.util.Set;
* ProjectName:Mirco-Service-Skeleton
*/
@Service
public
class
UserDetailsServiceImpl
implements
UserDetailsService
{
public
class
UserDetailsServiceImpl
implements
UserDetailsService
{
@Autowired
private
UserService
userService
;
@Override
public
UserDetails
loadUserByUsername
(
String
username
)
throws
UsernameNotFoundException
{
RcUserEntity
userEntity
=
userService
.
findByUsername
(
username
);
if
(
userEntity
==
null
){
if
(
userEntity
==
null
)
{
throw
new
UsernameNotFoundException
(
"用户:"
+
username
+
",不存在!"
);
}
Set
<
GrantedAuthority
>
grantedAuthorities
=
new
HashSet
<>();
User
user
=
new
User
(
userEntity
.
getUsername
(),
userEntity
.
getPassword
(),
grantedAuthorities
);
boolean
enabled
=
true
;
// 可用性 :true:可用 false:不可用
boolean
accountNonExpired
=
true
;
// 过期性 :true:没过期 false:过期
boolean
credentialsNonExpired
=
true
;
// 有效性 :true:凭证有效 false:凭证无效
boolean
accountNonLocked
=
true
;
// 锁定性 :true:未锁定 false:已锁定
User
user
=
new
User
(
userEntity
.
getUsername
(),
userEntity
.
getPassword
(),
enabled
,
accountNonExpired
,
credentialsNonExpired
,
accountNonLocked
,
grantedAuthorities
);
return
user
;
}
}
auth-center/src/main/resources/application.yml
浏览文件 @
1d2fac62
server
:
port
:
9060
spring
:
application
:
name
:
auth2.0-center
...
...
@@ -11,9 +13,10 @@ spring:
password
:
123456
druid
:
driver-class-name
:
com.mysql.jdbc.Driver
server
:
port
:
9060
redis
:
host
:
127.0.0.1
port
:
6379
password
:
123456
eureka
:
instance
:
prefer-ip-address
:
true
#使用IP注册
...
...
@@ -32,4 +35,8 @@ endpoints:
management
:
security
:
enabled
:
false
###actuator监控点 end####
\ No newline at end of file
###actuator监控点 end####
security
:
oauth2
:
resource
:
filter-order
:
3
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录