Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
X
xyqb-user2
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
head_group
xyqb-user2
Commits
700a9ed2
Commit
700a9ed2
authored
Mar 02, 2023
by
孙 楠
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
重构一键登录,删除无效类
parent
58f99877
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
80 additions
and
205 deletions
+80
-205
AuthenticationConfiguration.java
...n/quantgroup/xyqb/config/AuthenticationConfiguration.java
+0
-68
RestTemplateConfig.java
...va/cn/quantgroup/xyqb/config/http/RestTemplateConfig.java
+2
-2
AuthenticationAdapter.java
...va/cn/quantgroup/xyqb/security/AuthenticationAdapter.java
+0
-33
AuthenticationFactory.java
...va/cn/quantgroup/xyqb/security/AuthenticationFactory.java
+25
-0
AuthorizationAspect.java
...java/cn/quantgroup/xyqb/security/AuthorizationAspect.java
+23
-6
CustomerAuthenticationManager.java
...antgroup/xyqb/security/CustomerAuthenticationManager.java
+0
-61
UroraAuthenticationManager.java
.../cn/quantgroup/xyqb/urora/UroraAuthenticationManager.java
+29
-34
logback-dev.xml
src/main/resources/logback-dev.xml
+1
-1
No files found.
src/main/java/cn/quantgroup/xyqb/config/AuthenticationConfiguration.java
deleted
100644 → 0
View file @
58f99877
package
cn
.
quantgroup
.
xyqb
.
config
;
import
static
cn
.
quantgroup
.
xyqb
.
security
.
SecurityConstant
.
PROPERTY_PREFIX
;
import
cn.quantgroup.xyqb.security.AuthenticationAdapter
;
import
cn.quantgroup.xyqb.security.AuthenticationPrincipal
;
import
cn.quantgroup.xyqb.security.CustomerAuthenticationManager
;
import
cn.quantgroup.xyqb.urora.UroraAuthenticationManager
;
import
java.util.Map
;
import
java.util.Properties
;
import
java.util.Set
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.http.client.SimpleClientHttpRequestFactory
;
import
org.springframework.web.client.RestTemplate
;
/**
* Created by Administrator on 2021/7/1 0001.
*/
@Configuration
@EnableConfigurationProperties
(
AuthenticationConfigurationProperties
.
class
)
public
class
AuthenticationConfiguration
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
AuthenticationConfiguration
.
class
);
@Bean
public
AuthenticationAdapter
authenticationAdapter
(
AuthenticationConfigurationProperties
properties
)
{
AuthenticationAdapter
adapter
=
new
AuthenticationAdapter
();
Map
<
String
,
Map
<
String
,
String
>>
manager
=
properties
.
getManager
();
Set
<
String
>
strings
=
manager
.
keySet
();
try
{
for
(
String
key
:
strings
)
{
Map
<
String
,
String
>
map
=
manager
.
get
(
key
);
Class
<?>
target
=
Thread
.
currentThread
().
getContextClassLoader
().
loadClass
(
map
.
get
(
"target"
));
//TODO: wait bean bean definition authentication manager
CustomerAuthenticationManager
instance
=
(
CustomerAuthenticationManager
)
target
.
newInstance
();
instance
.
setPrincipal
(
new
AuthenticationPrincipal
(
key
));
Properties
prop
=
new
Properties
();
map
.
forEach
((
k
,
v
)
->
{
if
(
k
.
startsWith
(
PROPERTY_PREFIX
))
{
prop
.
setProperty
(
k
.
substring
(
PROPERTY_PREFIX
.
length
()),
v
);
}
});
instance
.
setProperties
(
prop
);
if
(
UroraAuthenticationManager
.
class
.
isInstance
(
instance
))
{
SimpleClientHttpRequestFactory
factory
=
new
SimpleClientHttpRequestFactory
();
factory
.
setReadTimeout
(
Integer
.
parseInt
(
prop
.
getProperty
(
"readTimeout"
,
"10000"
)));
factory
.
setConnectTimeout
(
Integer
.
parseInt
(
prop
.
getProperty
(
"connectTimeout"
,
"20000"
)));
RestTemplate
restTemplate
=
new
RestTemplate
();
restTemplate
.
setRequestFactory
(
factory
);
((
UroraAuthenticationManager
)
instance
).
setRestTemplate
(
restTemplate
);
}
instance
.
init
();
logger
.
info
(
"Authentication adapter register manager. {} - properties:{}"
,
target
,
prop
);
adapter
.
register
(
instance
);
}
}
catch
(
Exception
e
)
{
logger
.
error
(
"Authentication configuration initialization failed. "
+
e
.
getMessage
(),
e
);
}
return
adapter
;
}
}
src/main/java/cn/quantgroup/xyqb/config/http/RestTemplateConfig.java
View file @
700a9ed2
...
...
@@ -25,8 +25,8 @@ public class RestTemplateConfig {
@Bean
public
RestTemplate
restTemplate
()
{
SimpleClientHttpRequestFactory
requestFactory
=
new
SimpleClientHttpRequestFactory
();
requestFactory
.
setConnectTimeout
(
60
*
1000
);
requestFactory
.
setReadTimeout
(
60
*
1000
);
requestFactory
.
setConnectTimeout
(
5
*
1000
);
requestFactory
.
setReadTimeout
(
15
*
1000
);
RestTemplate
restTemplate
=
new
RestTemplate
(
requestFactory
);
return
restTemplate
;
...
...
src/main/java/cn/quantgroup/xyqb/security/AuthenticationAdapter.java
deleted
100644 → 0
View file @
58f99877
package
cn
.
quantgroup
.
xyqb
.
security
;
import
java.util.HashMap
;
import
java.util.Map
;
import
org.springframework.security.core.Authentication
;
/**
* Created by Administrator on 2021/7/1 0001.
*/
public
class
AuthenticationAdapter
{
public
Map
<
String
,
CustomerAuthenticationManager
>
managers
=
new
HashMap
<>();
public
AuthenticationAdapter
()
{
}
public
void
register
(
CustomerAuthenticationManager
manager
)
{
managers
.
put
(
manager
.
getPrincipal
().
getName
(),
manager
);
}
public
CustomerAuthenticationManager
getManager
(
String
name
){
return
managers
.
get
(
name
);
}
public
Authentication
authenticate
(
String
principal
,
String
credentials
){
CustomerAuthenticationManager
authenticationManager
=
getManager
(
principal
);
if
(
authenticationManager
==
null
){
throw
new
IllegalArgumentException
(
"No such authentication manager. "
+
principal
);
}
Authentication
authenticate
=
authenticationManager
.
authenticate
(
credentials
);
return
authenticate
;
}
}
src/main/java/cn/quantgroup/xyqb/security/AuthenticationFactory.java
0 → 100644
View file @
700a9ed2
package
cn
.
quantgroup
.
xyqb
.
security
;
import
cn.quantgroup.xyqb.util.ApplicationContextHolder
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.BeansException
;
import
org.springframework.security.authentication.AuthenticationManager
;
/**
* Created by Administrator on 2021/7/1 0001.
*/
@Slf4j
public
class
AuthenticationFactory
{
public
static
AuthenticationManager
getManager
(
String
name
){
AuthenticationManager
manager
=
null
;
try
{
manager
=
ApplicationContextHolder
.
getBean
(
name
.
concat
(
"AuthenticationManager"
),
AuthenticationManager
.
class
);
if
(
manager
==
null
)
{
throw
new
IllegalArgumentException
(
"No such authentication manager "
+
name
);
}
}
catch
(
BeansException
e
)
{
throw
new
IllegalArgumentException
(
"No such authentication manager "
+
name
);
}
return
manager
;
}
}
src/main/java/cn/quantgroup/xyqb/security/AuthorizationAspect.java
View file @
700a9ed2
package
cn
.
quantgroup
.
xyqb
.
security
;
import
cn.quantgroup.xyqb.urora.UroraAuthenticationToken
;
import
org.apache.commons.codec.binary.Base64
;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.aspectj.lang.annotation.Around
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.annotation.Pointcut
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.
beans.factory.annotation.Autowired
;
import
org.springframework.
security.authentication.AuthenticationManager
;
import
org.springframework.security.authentication.BadCredentialsException
;
import
org.springframework.security.core.Authentication
;
import
org.springframework.security.core.AuthenticationException
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.ObjectUtils
;
import
org.springframework.util.StringUtils
;
/**
* Created by Administrator on 2021/6/25 0025.
...
...
@@ -19,10 +24,6 @@ import org.springframework.stereotype.Component;
public
class
AuthorizationAspect
extends
HttpAspect
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
AuthorizationAspect
.
class
);
@Autowired
private
AuthenticationAdapter
authenticationAdapter
;
@Pointcut
(
"@annotation(cn.quantgroup.xyqb.security.AuthorizationPoint)"
)
private
void
pointCut
()
{
}
...
...
@@ -30,7 +31,8 @@ public class AuthorizationAspect extends HttpAspect {
@Around
(
"pointCut()"
)
private
Object
around
(
ProceedingJoinPoint
joinPoint
)
throws
Throwable
{
AssentHeader
header
=
getAssentHeader
();
Authentication
authenticate
=
authenticationAdapter
.
authenticate
(
header
.
getAuthType
(),
header
.
getAuthMessage
());
AuthenticationManager
manager
=
AuthenticationFactory
.
getManager
(
header
.
getAuthType
());
Authentication
authenticate
=
manager
.
authenticate
(
getCredentials
(
header
.
getAuthMessage
()));
if
(!
authenticate
.
isAuthenticated
()){
throw
new
BadCredentialsException
(
"not authorized"
);
}
...
...
@@ -42,4 +44,19 @@ public class AuthorizationAspect extends HttpAspect {
AuthorityManager
.
release
();
}
}
public
Authentication
getCredentials
(
String
credentials
)
throws
AuthenticationException
{
String
auth
=
ObjectUtils
.
getDisplayString
(
credentials
);
if
(
StringUtils
.
isEmpty
(
credentials
))
{
throw
new
BadCredentialsException
(
"Authorization credentials does not look like urora. "
+
credentials
);
}
auth
=
new
String
(
Base64
.
decodeBase64
(
auth
));
String
[]
strings
=
auth
.
split
(
":"
);
if
(
strings
.
length
!=
2
)
{
throw
new
BadCredentialsException
(
"Authorization credentials does not look like urora. "
+
auth
);
}
return
new
UroraAuthenticationToken
(
strings
[
0
],
strings
[
1
]);
}
}
src/main/java/cn/quantgroup/xyqb/security/CustomerAuthenticationManager.java
deleted
100644 → 0
View file @
58f99877
package
cn
.
quantgroup
.
xyqb
.
security
;
import
java.security.Principal
;
import
java.util.Properties
;
import
org.springframework.security.authentication.AuthenticationManager
;
import
org.springframework.security.authentication.BadCredentialsException
;
import
org.springframework.security.core.Authentication
;
import
org.springframework.security.core.AuthenticationException
;
import
org.springframework.util.StringUtils
;
/**
* Created by Administrator on 2021/7/1 0001.
*/
public
abstract
class
CustomerAuthenticationManager
implements
AuthenticationManager
{
private
Principal
principal
;
private
Properties
properties
=
new
Properties
();
public
CustomerAuthenticationManager
()
{
}
public
Properties
getProperties
()
{
return
properties
;
}
public
void
setProperties
(
Properties
properties
)
{
this
.
properties
=
properties
;
}
public
String
getProperty
(
String
key
)
{
return
properties
.
getProperty
(
key
);
}
public
void
setPrincipal
(
Principal
principal
)
{
this
.
principal
=
principal
;
}
public
Principal
getPrincipal
()
{
return
principal
;
}
public
Authentication
authenticate
(
String
credentials
)
throws
AuthenticationException
{
Authentication
auth
=
getCredentials
(
credentials
);
if
(
StringUtils
.
isEmpty
(
auth
.
getPrincipal
())
||
StringUtils
.
isEmpty
(
auth
.
getCredentials
()))
{
throw
new
BadCredentialsException
(
"authorization credentials does not look like urora. "
+
auth
);
}
auth
=
authenticate
(
auth
);
return
auth
;
}
public
abstract
void
init
();
public
abstract
Authentication
getCredentials
(
String
message
)
throws
AuthenticationException
;
public
abstract
Authentication
authenticate
(
Authentication
authentication
)
throws
AuthenticationException
;
}
src/main/java/cn/quantgroup/xyqb/urora/UroraAuthenticationManager.java
View file @
700a9ed2
package
cn
.
quantgroup
.
xyqb
.
urora
;
import
cn.quantgroup.xyqb.security.CustomerAuthenticationManager
;
import
cn.quantgroup.xyqb.security.RSADecrypt
;
import
java.util.Arrays
;
import
java.util.List
;
import
org.apache.commons.codec.binary.Base64
;
import
java.util.Objects
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpStatus
;
...
...
@@ -14,72 +16,65 @@ import org.springframework.http.MediaType;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.converter.HttpMessageConverter
;
import
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
;
import
org.springframework.security.authentication.
BadCredentialsException
;
import
org.springframework.security.authentication.
AuthenticationManager
;
import
org.springframework.security.core.Authentication
;
import
org.springframework.security.core.AuthenticationException
;
import
org.springframework.
util.ObjectUtils
;
import
org.springframework.
stereotype.Component
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.client.RestTemplate
;
import
javax.annotation.PostConstruct
;
/**
* Created by Administrator on 2021/7/1 0001.
*/
public
class
UroraAuthenticationManager
extends
CustomerAuthenticationManager
{
@Component
(
value
=
"UroraAuthenticationManager"
)
public
class
UroraAuthenticationManager
implements
AuthenticationManager
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
UroraAuthenticationManager
.
class
);
@Autowired
private
RestTemplate
restTemplate
;
private
HttpHeaders
headers
;
public
RestTemplate
getRestTemplate
()
{
return
restTemplate
;
}
@Value
(
"${user.auth.manager.Urora.properties.authorization}"
)
private
String
authorization
;
public
void
setRestTemplate
(
RestTemplate
restTemplate
)
{
this
.
restTemplate
=
restTemplate
;
}
@Value
(
"${user.auth.manager.Urora.properties.loginTokenVerify}"
)
private
String
appUrl
;
@Override
@Value
(
"${user.auth.manager.Urora.properties.loginTokenVerifyWeb:https://api.verification.jpush.cn/v1/web/h5/loginTokenVerify}"
)
private
String
webUrl
;
@Value
(
"${user.auth.manager.Urora.properties.privateKey}"
)
private
String
key
;
@PostConstruct
public
void
init
()
{
this
.
headers
=
new
HttpHeaders
();
this
.
headers
.
add
(
"Authorization"
,
getProperty
(
"authorization"
)
);
this
.
headers
.
add
(
"Authorization"
,
authorization
);
this
.
headers
.
setContentType
(
MediaType
.
APPLICATION_JSON_UTF8
);
List
<
HttpMessageConverter
<?>>
messageConverters
=
restTemplate
.
getMessageConverters
();
MappingJackson2HttpMessageConverter
stringHttpMessageConverter
=
new
MappingJackson2HttpMessageConverter
();
stringHttpMessageConverter
.
setSupportedMediaTypes
(
Arrays
.
asList
(
MediaType
.
APPLICATION_JSON
,
MediaType
.
APPLICATION_JSON_UTF8
,
MediaType
.
TEXT_PLAIN
));
messageConverters
.
add
(
stringHttpMessageConverter
);
}
@Override
public
Authentication
getCredentials
(
String
credentials
)
throws
AuthenticationException
{
String
auth
=
ObjectUtils
.
getDisplayString
(
credentials
);
if
(
StringUtils
.
isEmpty
(
credentials
))
{
throw
new
BadCredentialsException
(
"Authorization credentials does not look like urora. "
+
credentials
);
}
auth
=
new
String
(
Base64
.
decodeBase64
(
auth
));
String
[]
strings
=
auth
.
split
(
":"
);
if
(
strings
.
length
!=
2
)
{
throw
new
BadCredentialsException
(
"Authorization credentials does not look like urora. "
+
auth
);
}
return
new
UroraAuthenticationToken
(
strings
[
0
],
strings
[
1
]);
}
@Override
public
Authentication
authenticate
(
Authentication
authentication
)
throws
AuthenticationException
{
HttpEntity
<
String
>
httpEntity
=
new
HttpEntity
<>(
"{\"loginToken\":\""
+
authentication
.
getCredentials
()
+
"\"}"
,
headers
);
try
{
ResponseEntity
<
UroraResponse
>
verify
=
restTemplate
.
postForEntity
(
getProperty
(
"loginTokenVerify"
),
httpEntity
,
UroraResponse
.
class
);
String
url
=
appUrl
;
if
(
Objects
.
nonNull
(
authentication
.
getPrincipal
())
&&
(
authentication
.
getPrincipal
().
equals
(
"operatorWeb"
))){
url
=
webUrl
;
}
ResponseEntity
<
UroraResponse
>
verify
=
restTemplate
.
postForEntity
(
url
,
httpEntity
,
UroraResponse
.
class
);
if
(
HttpStatus
.
OK
.
equals
(
verify
.
getStatusCode
()))
{
UroraResponse
body
=
verify
.
getBody
();
if
(!
body
.
getCode
().
equals
(
"8000"
)
||
StringUtils
.
isEmpty
(
body
.
getPhone
()))
{
logger
.
warn
(
"Urora login token verify failed. {}"
,
body
);
authentication
.
setAuthenticated
(
false
);
}
else
{
String
decrypt
=
RSADecrypt
.
decrypt
(
body
.
getPhone
(),
getProperty
(
"privateKey"
)
);
String
decrypt
=
RSADecrypt
.
decrypt
(
body
.
getPhone
(),
key
);
UroraAuthenticationToken
token
=
new
UroraAuthenticationToken
(
body
.
getId
(),
decrypt
);
token
.
setAuthenticated
(
true
);
return
token
;
...
...
src/main/resources/logback-dev.xml
View file @
700a9ed2
...
...
@@ -66,7 +66,7 @@
<!--<logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="${logging.hibernate.level}"/>-->
<!--<logger name="org.hibernate.engine.QueryParameters" level="DEBUG"/>-->
<!--<logger name="org.hibernate.SQL" level="DEBUG" />-->
<root
level=
"
DEBUG
"
>
<root
level=
"
INFO
"
>
<appender-ref
ref=
"STDOUT"
/>
<appender-ref
ref=
"ROLLINGFILE"
/>
</root>
...
...
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