Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
loginservice
Overview
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
lanmw
loginservice
Commits
0f5bff1d
Commit
0f5bff1d
authored
Sep 02, 2019
by
zhangkb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
提交loginService修改代码。
parent
e4b4a2b6
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
360 additions
and
314 deletions
+360
-314
LoginApplication.java
src/main/java/com/keymobile/proxy/LoginApplication.java
+15
-13
LoginManagement.java
src/main/java/com/keymobile/proxy/api/LoginManagement.java
+49
-49
SecurityConfig.java
src/main/java/com/keymobile/proxy/conf/SecurityConfig.java
+90
-91
AuthService.java
src/main/java/com/keymobile/proxy/service/AuthService.java
+36
-34
CustomUserDetailsService.java
...com/keymobile/proxy/service/CustomUserDetailsService.java
+88
-89
application-test.yml
src/main/resources/application-test.yml
+78
-38
application.yml
src/main/resources/application.yml
+4
-0
No files found.
src/main/java/com/keymobile/proxy/LoginApplication.java
View file @
0f5bff1d
package
com
.
keymobile
.
proxy
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
@SpringBootApplication
public
class
LoginApplication
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
LoginApplication
.
class
,
args
);
}
}
package
com
.
keymobile
.
proxy
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.cloud.openfeign.EnableFeignClients
;
@SpringBootApplication
@EnableFeignClients
public
class
LoginApplication
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
LoginApplication
.
class
,
args
);
}
}
src/main/java/com/keymobile/proxy/api/LoginManagement.java
View file @
0f5bff1d
package
com
.
keymobile
.
proxy
.
api
;
import
org.springframework.security.core.context.SecurityContextHolder
;
import
org.springframework.security.core.userdetails.UserDetails
;
import
org.springframework.web.bind.annotation.*
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpSession
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
@RestController
@RequestMapping
(
value
=
"/"
)
public
class
LoginManagement
{
@RequestMapping
(
value
=
"/sessionInfo"
,
method
=
RequestMethod
.
POST
)
public
@ResponseBody
Map
<
String
,
Object
>
verifyLogin
(
HttpServletRequest
request
,
HttpServletResponse
response
)
{
UserDetails
userDetails
=
(
UserDetails
)
SecurityContextHolder
.
getContext
().
getAuthentication
().
getPrincipal
();
Map
<
String
,
Object
>
rs
=
new
HashMap
<>();
String
userNameWithIdAttached
=
userDetails
.
getUsername
();
rs
.
put
(
Constants
.
Session_UserName
,
userNameWithIdAttached
.
split
(
":"
)[
0
]);
rs
.
put
(
Constants
.
Session_UserId
,
userNameWithIdAttached
.
split
(
":"
)[
1
]);
rs
.
put
(
Constants
.
Session_UserDName
,
userNameWithIdAttached
.
split
(
":"
)[
2
]);
List
<
String
>
roles
=
new
ArrayList
<>();
userDetails
.
getAuthorities
().
forEach
(
auth
->
roles
.
add
(
auth
.
getAuthority
()));
rs
.
put
(
Constants
.
Session_Roles
,
roles
);
HttpSession
session
=
request
.
getSession
();
Object
lang
=
session
.
getAttribute
(
Constants
.
Session_Lang
);
rs
.
put
(
Constants
.
Session_Lang
,
lang
!=
null
?
lang
.
toString
()
:
"cn"
);
return
rs
;
}
@RequestMapping
(
value
=
"/lang"
,
method
=
RequestMethod
.
POST
)
public
String
setLANG
(
HttpServletRequest
request
,
@RequestParam
(
value
=
"LANG"
,
required
=
true
)
String
LANG
)
{
HttpSession
session
=
request
.
getSession
();
if
(!
LANG
.
equals
(
"en"
)
&&
!
LANG
.
equals
(
"cn"
))
session
.
setAttribute
(
Constants
.
Session_Lang
,
"cn"
);
else
session
.
setAttribute
(
Constants
.
Session_Lang
,
LANG
);
return
session
.
getAttribute
(
Constants
.
Session_Lang
).
toString
();
}
}
package
com
.
keymobile
.
proxy
.
api
;
import
org.springframework.security.core.context.SecurityContextHolder
;
import
org.springframework.security.core.userdetails.UserDetails
;
import
org.springframework.web.bind.annotation.*
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpSession
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
@RestController
@RequestMapping
(
value
=
"/"
)
public
class
LoginManagement
{
@RequestMapping
(
value
=
"/sessionInfo"
)
public
@ResponseBody
Map
<
String
,
Object
>
verifyLogin
(
HttpServletRequest
request
,
HttpServletResponse
response
)
{
UserDetails
userDetails
=
(
UserDetails
)
SecurityContextHolder
.
getContext
().
getAuthentication
().
getPrincipal
();
Map
<
String
,
Object
>
rs
=
new
HashMap
<>();
String
userNameWithIdAttached
=
userDetails
.
getUsername
();
rs
.
put
(
Constants
.
Session_UserName
,
userNameWithIdAttached
.
split
(
":"
)[
0
]);
rs
.
put
(
Constants
.
Session_UserId
,
userNameWithIdAttached
.
split
(
":"
)[
1
]);
rs
.
put
(
Constants
.
Session_UserDName
,
userNameWithIdAttached
.
split
(
":"
)[
2
]);
List
<
String
>
roles
=
new
ArrayList
<>();
userDetails
.
getAuthorities
().
forEach
(
auth
->
roles
.
add
(
auth
.
getAuthority
()));
rs
.
put
(
Constants
.
Session_Roles
,
roles
);
HttpSession
session
=
request
.
getSession
();
Object
lang
=
session
.
getAttribute
(
Constants
.
Session_Lang
);
rs
.
put
(
Constants
.
Session_Lang
,
lang
!=
null
?
lang
.
toString
()
:
"cn"
);
return
rs
;
}
@RequestMapping
(
value
=
"/lang"
,
method
=
RequestMethod
.
POST
)
public
String
setLANG
(
HttpServletRequest
request
,
@RequestParam
(
value
=
"LANG"
,
required
=
true
)
String
LANG
)
{
HttpSession
session
=
request
.
getSession
();
if
(!
LANG
.
equals
(
"en"
)
&&
!
LANG
.
equals
(
"cn"
))
session
.
setAttribute
(
Constants
.
Session_Lang
,
"cn"
);
else
session
.
setAttribute
(
Constants
.
Session_Lang
,
LANG
);
return
session
.
getAttribute
(
Constants
.
Session_Lang
).
toString
();
}
}
src/main/java/com/keymobile/proxy/conf/SecurityConfig.java
View file @
0f5bff1d
package
com
.
keymobile
.
proxy
.
conf
;
import
com.keymobile.proxy.model.CasProperties
;
import
com.keymobile.proxy.service.CustomUserDetailsService
;
import
org.jasig.cas.client.validation.Cas20ServiceTicketValidator
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.security.cas.ServiceProperties
;
import
org.springframework.security.cas.authentication.CasAssertionAuthenticationToken
;
import
org.springframework.security.cas.authentication.CasAuthenticationProvider
;
import
org.springframework.security.cas.web.CasAuthenticationEntryPoint
;
import
org.springframework.security.cas.web.CasAuthenticationFilter
;
import
org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
;
import
org.springframework.security.config.annotation.web.builders.HttpSecurity
;
import
org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
;
import
org.springframework.security.core.userdetails.AuthenticationUserDetailsService
;
@Configuration
public
class
SecurityConfig
extends
WebSecurityConfigurerAdapter
{
@Autowired
private
CasProperties
casProperties
;
@Autowired
public
void
configure
(
AuthenticationManagerBuilder
auth
)
throws
Exception
{
super
.
configure
(
auth
);
auth
.
authenticationProvider
(
casAuthenticationProvider
());
}
@Override
protected
void
configure
(
HttpSecurity
http
)
throws
Exception
{
http
.
authorizeRequests
().
antMatchers
(
"/api/info/**/*"
).
authenticated
()
.
antMatchers
(
"/api/**/*"
).
permitAll
()
.
anyRequest
().
authenticated
();
http
.
exceptionHandling
().
authenticationEntryPoint
(
casAuthenticationEntryPoint
()).
and
()
.
addFilter
(
casAuthenticationFilter
());
http
.
csrf
().
disable
();
}
/**认证的入口*/
@Bean
public
CasAuthenticationEntryPoint
casAuthenticationEntryPoint
()
{
CasAuthenticationEntryPoint
casAuthenticationEntryPoint
=
new
CasAuthenticationEntryPoint
();
casAuthenticationEntryPoint
.
setLoginUrl
(
casProperties
.
getCasServerLoginUrl
());
casAuthenticationEntryPoint
.
setServiceProperties
(
serviceProperties
());
return
casAuthenticationEntryPoint
;
}
/**CAS认证过滤器*/
@Bean
public
CasAuthenticationFilter
casAuthenticationFilter
()
throws
Exception
{
CasAuthenticationFilter
casAuthenticationFilter
=
new
CasAuthenticationFilter
();
casAuthenticationFilter
.
setAuthenticationManager
(
authenticationManager
());
casAuthenticationFilter
.
setFilterProcessesUrl
(
casProperties
.
getAppLoginUrl
());
return
casAuthenticationFilter
;
}
@Bean
public
CasAuthenticationProvider
casAuthenticationProvider
()
{
CasAuthenticationProvider
casAuthenticationProvider
=
new
CasAuthenticationProvider
();
casAuthenticationProvider
.
setAuthenticationUserDetailsService
(
customUserDetailsService
());
//casAuthenticationProvider.setUserDetailsService(customUserDetailsService()); //这里只是接口类型,实现的接口不一样,都可以的。
casAuthenticationProvider
.
setServiceProperties
(
serviceProperties
());
casAuthenticationProvider
.
setTicketValidator
(
cas20ServiceTicketValidator
());
casAuthenticationProvider
.
setKey
(
"casAuthenticationProviderKey"
);
return
casAuthenticationProvider
;
}
/**指定service相关信息*/
@Bean
public
ServiceProperties
serviceProperties
()
{
ServiceProperties
serviceProperties
=
new
ServiceProperties
();
serviceProperties
.
setService
(
casProperties
.
getAppServerUrl
()
+
casProperties
.
getAppLoginUrl
());
serviceProperties
.
setAuthenticateAllArtifacts
(
true
);
return
serviceProperties
;
}
@Bean
public
Cas20ServiceTicketValidator
cas20ServiceTicketValidator
()
{
return
new
Cas20ServiceTicketValidator
(
casProperties
.
getCasServerUrl
());
}
/**用户自定义的AuthenticationUserDetailsService*/
@Bean
public
AuthenticationUserDetailsService
<
CasAssertionAuthenticationToken
>
customUserDetailsService
(){
return
new
CustomUserDetailsService
();
}
}
package
com
.
keymobile
.
proxy
.
conf
;
import
com.keymobile.proxy.model.CasProperties
;
import
com.keymobile.proxy.service.CustomUserDetailsService
;
import
org.jasig.cas.client.validation.Cas20ServiceTicketValidator
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.security.cas.ServiceProperties
;
import
org.springframework.security.cas.authentication.CasAssertionAuthenticationToken
;
import
org.springframework.security.cas.authentication.CasAuthenticationProvider
;
import
org.springframework.security.cas.web.CasAuthenticationEntryPoint
;
import
org.springframework.security.cas.web.CasAuthenticationFilter
;
import
org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
;
import
org.springframework.security.config.annotation.web.builders.HttpSecurity
;
import
org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
;
import
org.springframework.security.core.userdetails.AuthenticationUserDetailsService
;
@Configuration
public
class
SecurityConfig
extends
WebSecurityConfigurerAdapter
{
@Autowired
private
CasProperties
casProperties
;
@Override
public
void
configure
(
AuthenticationManagerBuilder
auth
)
throws
Exception
{
super
.
configure
(
auth
);
auth
.
authenticationProvider
(
casAuthenticationProvider
());
}
@Override
protected
void
configure
(
HttpSecurity
http
)
throws
Exception
{
http
.
authorizeRequests
()
.
anyRequest
().
authenticated
();
http
.
exceptionHandling
().
authenticationEntryPoint
(
casAuthenticationEntryPoint
()).
and
()
.
addFilter
(
casAuthenticationFilter
());
http
.
csrf
().
disable
();
}
/**认证的入口*/
@Bean
public
CasAuthenticationEntryPoint
casAuthenticationEntryPoint
()
{
CasAuthenticationEntryPoint
casAuthenticationEntryPoint
=
new
CasAuthenticationEntryPoint
();
casAuthenticationEntryPoint
.
setLoginUrl
(
casProperties
.
getCasServerLoginUrl
());
casAuthenticationEntryPoint
.
setServiceProperties
(
serviceProperties
());
return
casAuthenticationEntryPoint
;
}
/**CAS认证过滤器*/
@Bean
public
CasAuthenticationFilter
casAuthenticationFilter
()
throws
Exception
{
CasAuthenticationFilter
casAuthenticationFilter
=
new
CasAuthenticationFilter
();
casAuthenticationFilter
.
setAuthenticationManager
(
authenticationManager
());
casAuthenticationFilter
.
setFilterProcessesUrl
(
casProperties
.
getAppLoginUrl
());
return
casAuthenticationFilter
;
}
@Bean
public
CasAuthenticationProvider
casAuthenticationProvider
()
{
CasAuthenticationProvider
casAuthenticationProvider
=
new
CasAuthenticationProvider
();
casAuthenticationProvider
.
setAuthenticationUserDetailsService
(
customUserDetailsService
());
//casAuthenticationProvider.setUserDetailsService(customUserDetailsService()); //这里只是接口类型,实现的接口不一样,都可以的。
casAuthenticationProvider
.
setServiceProperties
(
serviceProperties
());
casAuthenticationProvider
.
setTicketValidator
(
cas20ServiceTicketValidator
());
casAuthenticationProvider
.
setKey
(
"casAuthenticationProviderKey"
);
return
casAuthenticationProvider
;
}
/**指定service相关信息*/
@Bean
public
ServiceProperties
serviceProperties
()
{
ServiceProperties
serviceProperties
=
new
ServiceProperties
();
serviceProperties
.
setService
(
casProperties
.
getAppServerUrl
()
+
casProperties
.
getAppLoginUrl
());
serviceProperties
.
setAuthenticateAllArtifacts
(
true
);
return
serviceProperties
;
}
@Bean
public
Cas20ServiceTicketValidator
cas20ServiceTicketValidator
()
{
return
new
Cas20ServiceTicketValidator
(
casProperties
.
getCasServerUrl
());
}
/**用户自定义的AuthenticationUserDetailsService*/
@Bean
public
AuthenticationUserDetailsService
<
CasAssertionAuthenticationToken
>
customUserDetailsService
(){
return
new
CustomUserDetailsService
();
}
}
src/main/java/com/keymobile/proxy/service/AuthService.java
View file @
0f5bff1d
package
com
.
keymobile
.
proxy
.
service
;
import
com.keymobile.proxy.model.Author
;
import
com.keymobile.proxy.model.Domain
;
import
com.keymobile.proxy.model.Role
;
import
com.keymobile.proxy.model.User
;
import
org.springframework.cloud.openfeign.FeignClient
;
import
org.springframework.web.bind.annotation.*
;
import
java.util.List
;
@FeignClient
(
value
=
"authService"
)
public
interface
AuthService
{
@RequestMapping
(
value
=
"/user"
)
User
getUserByName
(
@RequestParam
(
value
=
"name"
)
String
name
);
@PostMapping
(
value
=
"/users/{userId}"
)
User
updateUser
(
@PathVariable
(
value
=
"userId"
)
Long
userId
,
@RequestBody
User
user
);
@RequestMapping
(
value
=
"/users"
,
method
=
RequestMethod
.
POST
)
User
addUser
(
@RequestParam
(
value
=
"roleIds"
,
required
=
false
)
Long
[]
roleIds
,
@RequestParam
(
value
=
"domainIds"
,
required
=
false
)
Long
[]
domainIds
,
@RequestBody
User
user
);
@RequestMapping
(
value
=
"/roles/{roleId}/authors"
,
method
=
RequestMethod
.
GET
)
List
<
Author
>
getAuthorsOfRole
(
@PathVariable
(
value
=
"roleId"
)
Long
roleId
);
@RequestMapping
(
value
=
"/users/{userId}/roles"
,
method
=
RequestMethod
.
GET
)
List
<
Role
>
getRolesOfUser
(
@PathVariable
(
value
=
"userId"
)
Long
userId
);
@RequestMapping
(
value
=
"/users/{userId}/domains"
,
method
=
RequestMethod
.
GET
)
List
<
Domain
>
getDomainsOfUser
(
@PathVariable
(
value
=
"userId"
)
Long
userId
);
}
package
com
.
keymobile
.
proxy
.
service
;
import
com.keymobile.proxy.model.Author
;
import
com.keymobile.proxy.model.Domain
;
import
com.keymobile.proxy.model.Role
;
import
com.keymobile.proxy.model.User
;
import
org.springframework.cloud.openfeign.FeignClient
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.bind.annotation.*
;
import
java.util.List
;
@Component
@FeignClient
(
value
=
"authService"
)
public
interface
AuthService
{
@RequestMapping
(
value
=
"/user"
)
User
getUserByName
(
@RequestParam
(
value
=
"name"
)
String
name
);
@PostMapping
(
value
=
"/users/{userId}"
)
User
updateUser
(
@PathVariable
(
value
=
"userId"
)
Long
userId
,
@RequestBody
User
user
);
@RequestMapping
(
value
=
"/users"
,
method
=
RequestMethod
.
POST
)
User
addUser
(
@RequestParam
(
value
=
"roleIds"
,
required
=
false
)
Long
[]
roleIds
,
@RequestParam
(
value
=
"domainIds"
,
required
=
false
)
Long
[]
domainIds
,
@RequestBody
User
user
);
@RequestMapping
(
value
=
"/roles/{roleId}/authors"
,
method
=
RequestMethod
.
GET
)
List
<
Author
>
getAuthorsOfRole
(
@PathVariable
(
value
=
"roleId"
)
Long
roleId
);
@RequestMapping
(
value
=
"/users/{userId}/roles"
,
method
=
RequestMethod
.
GET
)
List
<
Role
>
getRolesOfUser
(
@PathVariable
(
value
=
"userId"
)
Long
userId
);
@RequestMapping
(
value
=
"/users/{userId}/domains"
,
method
=
RequestMethod
.
GET
)
List
<
Domain
>
getDomainsOfUser
(
@PathVariable
(
value
=
"userId"
)
Long
userId
);
}
src/main/java/com/keymobile/proxy/service/CustomUserDetailsService.java
View file @
0f5bff1d
package
com
.
keymobile
.
proxy
.
service
;
import
com.keymobile.proxy.api.Constants
;
import
com.keymobile.proxy.model.Author
;
import
com.keymobile.proxy.model.Domain
;
import
com.keymobile.proxy.model.Role
;
import
com.keymobile.proxy.util.HttpUtil
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.security.cas.authentication.CasAssertionAuthenticationToken
;
import
org.springframework.security.core.GrantedAuthority
;
import
org.springframework.security.core.authority.SimpleGrantedAuthority
;
import
org.springframework.security.core.userdetails.AuthenticationUserDetailsService
;
import
org.springframework.security.core.userdetails.User
;
import
org.springframework.security.core.userdetails.UserDetails
;
import
org.springframework.security.core.userdetails.UsernameNotFoundException
;
import
org.springframework.util.StringUtils
;
import
javax.servlet.http.HttpSession
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* 用于加载用户信息 实现UserDetailsService接口,或者实现AuthenticationUserDetailsService接口
*
*/
public
class
CustomUserDetailsService
//实现AuthenticationUserDetailsService,实现loadUserDetails方法
implements
AuthenticationUserDetailsService
<
CasAssertionAuthenticationToken
>
{
@Autowired
private
AuthService
authService
;
private
Logger
logger
=
LoggerFactory
.
getLogger
(
CustomUserDetailsService
.
class
);
@Override
public
UserDetails
loadUserDetails
(
CasAssertionAuthenticationToken
token
)
throws
UsernameNotFoundException
{
System
.
out
.
println
(
"当前的用户名是:"
+
token
.
getName
());
com
.
keymobile
.
proxy
.
model
.
User
u
=
this
.
authService
.
getUserByName
(
token
.
getName
());
if
(
u
==
null
)
{
u
=
new
com
.
keymobile
.
proxy
.
model
.
User
();
u
.
setName
(
token
.
getName
());
u
.
setPassword
(
"37fa265330ad83eaa879efb1e2db6380896cf639"
);
u
.
setDName
(
token
.
getName
());
u
=
this
.
authService
.
addUser
(
new
Long
[]
{
(
long
)
4
},
new
Long
[]
{},
u
);
this
.
logger
.
info
(
u
==
null
?
"u is null"
:
u
.
toString
());
}
List
<
GrantedAuthority
>
authorities
=
new
ArrayList
<>();
String
userDomainFilterStr
=
"*"
;
List
<
String
>
userDomainList
=
new
ArrayList
<>();
List
<
Domain
>
domainsOfUser
=
authService
.
getDomainsOfUser
(
u
.
getId
());
domainsOfUser
.
forEach
(
d
->
userDomainList
.
add
(
d
.
getDomainId
().
toString
()));
if
(
userDomainList
.
size
()
>
0
)
{
userDomainFilterStr
=
String
.
join
(
","
,
userDomainList
);
}
List
<
Role
>
rolesOfUser
=
authService
.
getRolesOfUser
(
u
.
getId
());
for
(
Role
role
:
rolesOfUser
)
{
List
<
Author
>
authors
=
authService
.
getAuthorsOfRole
(
role
.
getRoleId
());
for
(
Author
author:
authors
)
{
GrantedAuthority
authorityInfo
=
new
SimpleGrantedAuthority
(
Constants
.
ROLE_PREFIX
+
author
.
getAuthorName
()
+
":"
+
userDomainFilterStr
);
authorities
.
add
(
authorityInfo
);
}
}
List
<
String
>
roles
=
new
ArrayList
<>();
authorities
.
forEach
(
auth
->
roles
.
add
(
auth
.
getAuthority
()));
HttpSession
session
=
HttpUtil
.
getSession
();
if
(
session
!=
null
)
{
session
.
setAttribute
(
Constants
.
Session_UserId
,
u
.
getId
());
session
.
setAttribute
(
Constants
.
Session_UserName
,
u
.
getName
());
session
.
setAttribute
(
Constants
.
Session_UserDName
,
u
.
getDName
());
session
.
setAttribute
(
Constants
.
Session_Roles
,
roles
);
}
return
new
User
(
u
.
getName
()+
":"
+
u
.
getId
()
+
":"
+
u
.
getDName
(),
"37fa265330ad83eaa879efb1e2db6380896cf639"
,
authorities
);
}
}
package
com
.
keymobile
.
proxy
.
service
;
import
com.keymobile.proxy.api.Constants
;
import
com.keymobile.proxy.model.Author
;
import
com.keymobile.proxy.model.Domain
;
import
com.keymobile.proxy.model.Role
;
import
com.keymobile.proxy.util.HttpUtil
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.security.cas.authentication.CasAssertionAuthenticationToken
;
import
org.springframework.security.core.GrantedAuthority
;
import
org.springframework.security.core.authority.SimpleGrantedAuthority
;
import
org.springframework.security.core.userdetails.AuthenticationUserDetailsService
;
import
org.springframework.security.core.userdetails.User
;
import
org.springframework.security.core.userdetails.UserDetails
;
import
org.springframework.security.core.userdetails.UsernameNotFoundException
;
import
javax.servlet.http.HttpSession
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* 用于加载用户信息 实现UserDetailsService接口,或者实现AuthenticationUserDetailsService接口
*
*/
public
class
CustomUserDetailsService
//实现AuthenticationUserDetailsService,实现loadUserDetails方法
implements
AuthenticationUserDetailsService
<
CasAssertionAuthenticationToken
>
{
@Autowired
private
AuthService
authService
;
private
Logger
logger
=
LoggerFactory
.
getLogger
(
CustomUserDetailsService
.
class
);
@Override
public
UserDetails
loadUserDetails
(
CasAssertionAuthenticationToken
token
)
throws
UsernameNotFoundException
{
System
.
out
.
println
(
"当前的用户名是:"
+
token
.
getName
());
com
.
keymobile
.
proxy
.
model
.
User
u
=
this
.
authService
.
getUserByName
(
token
.
getName
());
if
(
u
==
null
)
{
u
=
new
com
.
keymobile
.
proxy
.
model
.
User
();
u
.
setName
(
token
.
getName
());
u
.
setPassword
(
"37fa265330ad83eaa879efb1e2db6380896cf639"
);
u
.
setDName
(
token
.
getName
());
u
=
this
.
authService
.
addUser
(
new
Long
[]
{
(
long
)
4
},
new
Long
[]
{},
u
);
this
.
logger
.
info
(
u
==
null
?
"u is null"
:
u
.
toString
());
}
List
<
GrantedAuthority
>
authorities
=
new
ArrayList
<>();
String
userDomainFilterStr
=
"*"
;
List
<
String
>
userDomainList
=
new
ArrayList
<>();
List
<
Domain
>
domainsOfUser
=
authService
.
getDomainsOfUser
(
u
.
getId
());
domainsOfUser
.
forEach
(
d
->
userDomainList
.
add
(
d
.
getDomainId
().
toString
()));
if
(
userDomainList
.
size
()
>
0
)
{
userDomainFilterStr
=
String
.
join
(
","
,
userDomainList
);
}
List
<
Role
>
rolesOfUser
=
authService
.
getRolesOfUser
(
u
.
getId
());
for
(
Role
role
:
rolesOfUser
)
{
List
<
Author
>
authors
=
authService
.
getAuthorsOfRole
(
role
.
getRoleId
());
for
(
Author
author:
authors
)
{
GrantedAuthority
authorityInfo
=
new
SimpleGrantedAuthority
(
Constants
.
ROLE_PREFIX
+
author
.
getAuthorName
()
+
":"
+
userDomainFilterStr
);
authorities
.
add
(
authorityInfo
);
}
}
List
<
String
>
roles
=
new
ArrayList
<>();
authorities
.
forEach
(
auth
->
roles
.
add
(
auth
.
getAuthority
()));
HttpSession
session
=
HttpUtil
.
getSession
();
if
(
session
!=
null
)
{
session
.
setAttribute
(
Constants
.
Session_UserId
,
u
.
getId
());
session
.
setAttribute
(
Constants
.
Session_UserName
,
u
.
getName
());
session
.
setAttribute
(
Constants
.
Session_UserDName
,
u
.
getDName
());
session
.
setAttribute
(
Constants
.
Session_Roles
,
roles
);
}
return
new
User
(
u
.
getName
()+
":"
+
u
.
getId
()
+
":"
+
u
.
getDName
(),
"37fa265330ad83eaa879efb1e2db6380896cf639"
,
authorities
);
}
}
src/main/resources/application-test.yml
View file @
0f5bff1d
server
:
port
:
8764
spring
:
application
:
name
:
auth
session
:
store-type
:
redis
redis
:
namespace
:
dataplatformdev
redis
:
host
:
localhost
port
:
6379
datasource
:
url
:
jdbc:mysql://localhost:3306/dataSharing?autoReconnect=true
username
:
root
password
:
dataSharing
servlet
:
multipart
:
max-file-size
:
100Mb
max-request-size
:
100Mb
eureka
:
client
:
registerWithEureka
:
false
region
:
default
registryFetchIntervalSeconds
:
5
serviceUrl
:
defaultZone
:
http://localhost:8081/eureka/
zuul
:
prefix
:
/api
sensitive-headers
:
logging
:
level
:
org.springframework.security
:
DEBUG
\ No newline at end of file
server
:
port
:
8777
spring
:
application
:
name
:
loginService
session
:
store-type
:
redis
redis
:
namespace
:
dataplatformtest
redis
:
cluster
:
nodes
:
192.168.0.192:6379
max-redirects
:
6
timeout
:
10000
#客户端超时时间单位是毫秒 默认是2000
maxIdle
:
300
#最大空闲数
maxTotal
:
1000
#控制一个pool可分配多少个jedis实例,用来替换上面的redis.maxActive,如果是jedis 2.4以后用该属性
maxWaitMillis
:
1000
#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
minEvictableIdleTimeMillis
:
300000
#连接的最小空闲时间 默认1800000毫秒(30分钟)
numTestsPerEvictionRun
:
1024
#每次释放连接的最大数目,默认3
timeBetweenEvictionRunsMillis
:
30000
#逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
testOnBorrow
:
true
#是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
testWhileIdle
:
true
#在空闲时检查有效性, 默认false
password
:
#密码
jpa
:
hibernate
:
ddl-auto
:
update
datasource
:
url
:
jdbc:mysql://dev-vm-00:3306/dev0?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
username
:
test
password
:
test
hikari
:
maximum-pool-size
:
5
servlet
:
multipart
:
max-file-size
:
100Mb
max-request-size
:
100Mb
eureka
:
client
:
registerWithEureka
:
true
region
:
default
registryFetchIntervalSeconds
:
5
serviceUrl
:
defaultZone
:
http://192.168.0.213:8081/eureka/
enabled
:
true
instance
:
prefer-ip-address
:
false
hostname
:
192.168.0.128
zuul
:
prefix
:
/api
sensitive-headers
:
logging
:
level
:
org.springframework.security
:
DEBUG
ribbon
:
ReadTimeout
:
60000
ConnectTimeout
:
60000
redirect-url
:
system-management
:
http://192.168.0.213:8089/views/login.html
security
:
authUser
:
root
authPwd
:
pwd
cas
:
server
:
base-url
:
http://192.168.253.128:8080/cas
paths
:
login
:
http://192.168.253.128:8080/cas/login
service
:
base-url
:
http://192.168.0.213:9090/center-home/view
paths
:
login
:
http://192.168.0.213:9090/center-home/view/main
\ No newline at end of file
src/main/resources/application.yml
0 → 100644
View file @
0f5bff1d
spring
:
profiles
:
active
:
${spring.profiles.active:test}
\ No newline at end of file
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