Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
guns-vip
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
chenjunxiong
guns-vip
Commits
44b0c1af
Commit
44b0c1af
authored
Aug 27, 2017
by
naan1993
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
完善验证器
parent
40499458
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
55 additions
and
38 deletions
+55
-38
guns-rest/src/main/java/com/stylefeng/guns/rest/modular/auth/controller/AuthController.java
+4
-6
guns-rest/src/main/java/com/stylefeng/guns/rest/modular/auth/controller/dto/AuthRequest.java
+17
-18
guns-rest/src/main/java/com/stylefeng/guns/rest/modular/auth/validator/IReqValidator.java
+3
-3
guns-rest/src/main/java/com/stylefeng/guns/rest/modular/auth/validator/dto/Credence.java
+20
-0
guns-rest/src/main/java/com/stylefeng/guns/rest/modular/auth/validator/impl/DbValidator.java
+5
-4
guns-rest/src/main/java/com/stylefeng/guns/rest/modular/auth/validator/impl/SimpleValidator.java
+6
-7
No files found.
guns-rest/src/main/java/com/stylefeng/guns/rest/modular/auth/controller/AuthController.java
View file @
44b0c1af
...
...
@@ -2,18 +2,16 @@ package com.stylefeng.guns.rest.modular.auth.controller;
import
com.stylefeng.guns.rest.common.exception.BizExceptionEnum
;
import
com.stylefeng.guns.rest.common.exception.BussinessException
;
import
com.stylefeng.guns.rest.modular.auth.controller.dto.AuthRequest
;
import
com.stylefeng.guns.rest.modular.auth.controller.dto.AuthResponse
;
import
com.stylefeng.guns.rest.modular.auth.util.JwtTokenUtil
;
import
com.stylefeng.guns.rest.modular.auth.validator.IReqValidator
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RestController
;
import
javax.annotation.Resource
;
import
javax.servlet.http.HttpServletRequest
;
import
java.util.Map
;
/**
* 请求验证的
...
...
@@ -31,13 +29,13 @@ public class AuthController {
private
IReqValidator
reqValidator
;
@RequestMapping
(
value
=
"${jwt.auth-path}"
)
public
ResponseEntity
<?>
createAuthenticationToken
(
@RequestParam
Map
<
String
,
Object
>
params
,
HttpServletRequest
r
equest
)
{
public
ResponseEntity
<?>
createAuthenticationToken
(
AuthRequest
authR
equest
)
{
boolean
validate
=
reqValidator
.
validate
(
params
);
boolean
validate
=
reqValidator
.
validate
(
authRequest
);
if
(
validate
)
{
final
String
randomKey
=
jwtTokenUtil
.
getRandomKey
();
final
String
token
=
jwtTokenUtil
.
generateToken
(
(
String
)
params
.
get
(
"userName"
),
randomKey
);
final
String
token
=
jwtTokenUtil
.
generateToken
(
authRequest
.
getUserName
(
),
randomKey
);
return
ResponseEntity
.
ok
(
new
AuthResponse
(
token
,
randomKey
));
}
else
{
throw
new
BussinessException
(
BizExceptionEnum
.
AUTH_REQUEST_ERROR
);
...
...
guns-rest/src/main/java/com/stylefeng/guns/rest/modular/auth/controller/dto/AuthRequest.java
View file @
44b0c1af
package
com
.
stylefeng
.
guns
.
rest
.
modular
.
auth
.
controller
.
dto
;
import
java.io.Serializabl
e
;
import
com.stylefeng.guns.rest.modular.auth.validator.dto.Credenc
e
;
/**
* 认证的请求dto
...
...
@@ -8,35 +8,34 @@ import java.io.Serializable;
* @author fengshuonan
* @Date 2017/8/24 14:00
*/
public
class
AuthRequest
implements
Serializabl
e
{
public
class
AuthRequest
implements
Credenc
e
{
private
static
final
long
serialVersionUID
=
-
8445943548965154778L
;
private
String
username
;
private
String
userName
;
private
String
password
;
public
AuthRequest
(
)
{
super
()
;
public
void
setUserName
(
String
userName
)
{
this
.
userName
=
userName
;
}
public
AuthRequest
(
String
username
,
String
password
)
{
this
.
setUsername
(
username
);
this
.
setPassword
(
password
);
public
String
getPassword
()
{
return
password
;
}
public
String
getUsername
(
)
{
return
this
.
username
;
public
void
setPassword
(
String
password
)
{
this
.
password
=
password
;
}
public
void
setUsername
(
String
username
)
{
this
.
username
=
usern
ame
;
public
String
getUserName
(
)
{
return
userN
ame
;
}
public
String
getPassword
()
{
return
this
.
password
;
@Override
public
String
getCredenceName
()
{
return
this
.
userName
;
}
public
void
setPassword
(
String
password
)
{
this
.
password
=
password
;
@Override
public
String
getCredenceCode
()
{
return
this
.
password
;
}
}
guns-rest/src/main/java/com/stylefeng/guns/rest/modular/auth/validator/IReqValidator.java
View file @
44b0c1af
package
com
.
stylefeng
.
guns
.
rest
.
modular
.
auth
.
validator
;
import
java.util.Map
;
import
com.stylefeng.guns.rest.modular.auth.validator.dto.Credence
;
/**
* <p>验证请求/auth接口时,请求参数的正确性</p>
*
*
<p>
* <p>如果想拓展验证方法只需实现这个接口,然后在AuthenticationRestController类中注意相应实现的本接口的类即可</p>
*
* @author fengshuonan
...
...
@@ -18,5 +18,5 @@ public interface IReqValidator {
* @author fengshuonan
* @Date 2017/8/23 11:49
*/
boolean
validate
(
Map
<
String
,
Object
>
params
);
boolean
validate
(
Credence
credence
);
}
guns-rest/src/main/java/com/stylefeng/guns/rest/modular/auth/validator/dto/Credence.java
0 → 100644
View file @
44b0c1af
package
com
.
stylefeng
.
guns
.
rest
.
modular
.
auth
.
validator
.
dto
;
/**
* 验证的凭据
*
* @author fengshuonan
* @date 2017-08-27 13:27
*/
public
interface
Credence
{
/**
* 凭据名称
*/
String
getCredenceName
();
/**
* 密码或者是其他的验证码之类的
*/
String
getCredenceCode
();
}
guns-rest/src/main/java/com/stylefeng/guns/rest/modular/auth/validator/impl/DbValidator.java
View file @
44b0c1af
...
...
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.mapper.EntityWrapper;
import
com.stylefeng.guns.rest.common.persistence.dao.UserMapper
;
import
com.stylefeng.guns.rest.common.persistence.model.User
;
import
com.stylefeng.guns.rest.modular.auth.validator.IReqValidator
;
import
com.stylefeng.guns.rest.modular.auth.validator.dto.Credence
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
...
...
@@ -23,11 +24,11 @@ public class DbValidator implements IReqValidator {
UserMapper
userMapper
;
@Override
public
boolean
validate
(
Map
<
String
,
Object
>
params
)
{
List
<
User
>
users
=
userMapper
.
selectList
(
new
EntityWrapper
<
User
>().
eq
(
"userName"
,
params
.
get
(
"userName"
)));
if
(
users
!=
null
&&
users
.
size
()
>
0
)
{
public
boolean
validate
(
Credence
credence
)
{
List
<
User
>
users
=
userMapper
.
selectList
(
new
EntityWrapper
<
User
>().
eq
(
"userName"
,
credence
.
getCredenceName
(
)));
if
(
users
!=
null
&&
users
.
size
()
>
0
)
{
return
true
;
}
else
{
}
else
{
return
false
;
}
}
...
...
guns-rest/src/main/java/com/stylefeng/guns/rest/modular/auth/validator/impl/SimpleValidator.java
View file @
44b0c1af
package
com
.
stylefeng
.
guns
.
rest
.
modular
.
auth
.
validator
.
impl
;
import
com.stylefeng.guns.rest.modular.auth.validator.IReqValidator
;
import
com.stylefeng.guns.rest.modular.auth.validator.dto.Credence
;
import
org.springframework.stereotype.Service
;
import
java.util.Map
;
/**
* 直接验证账号密码是不是admin
*
...
...
@@ -19,14 +18,14 @@ public class SimpleValidator implements IReqValidator {
private
static
String
PASSWORD
=
"admin"
;
@Override
public
boolean
validate
(
Map
<
String
,
Object
>
params
)
{
public
boolean
validate
(
Credence
credence
)
{
String
userName
=
(
String
)
params
.
get
(
"userName"
);
String
password
=
(
String
)
params
.
get
(
"password"
);
String
userName
=
credence
.
getCredenceName
(
);
String
password
=
credence
.
getCredenceCode
(
);
if
(
USER_NAME
.
equals
(
userName
)
&&
PASSWORD
.
equals
(
password
))
{
if
(
USER_NAME
.
equals
(
userName
)
&&
PASSWORD
.
equals
(
password
))
{
return
true
;
}
else
{
}
else
{
return
false
;
}
}
...
...
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