#103 初步完成双重验证

This commit is contained in:
zhouhao
2018-12-04 18:39:04 +08:00
parent 106006cf10
commit 768033f221
46 changed files with 1459 additions and 94 deletions

View File

@@ -51,6 +51,29 @@ class FullFunctionTest extends Specification {
}
def "测试双重验证"() {
given: "登录"
def token = doLogin("admin", "admin")
when: "登录成功"
token != null
then: "调用双重验证接口"
mockMvc.perform(get("/test/two-factor")
.header("token", token))
.andExpect(status().is(403))
.andReturn()
.getResponse()
.getContentAsString()
def resp = mockMvc.perform(get("/test/two-factor")
.header("token", token)
.param("verifyCode", "test"))
.andExpect(status().is(200))
.andReturn()
.getResponse()
.getContentAsString()
expect:
resp != null
}
def "测试查询"() {
given: "登录"
def token = doLogin("admin", "admin")

View File

@@ -1,8 +1,10 @@
package org.hswebframework.web.authorization.full.controller;
import org.hswebframework.web.authorization.annotation.Authorize;
import org.hswebframework.web.authorization.annotation.TwoFactor;
import org.hswebframework.web.authorization.full.controller.model.TestModel;
import org.hswebframework.web.controller.message.ResponseMessage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -21,4 +23,10 @@ public class TestCrudController implements CrudController<TestModel> {
return ResponseMessage.ok();
}
@TwoFactor(value = "test", provider = "test")
@GetMapping("/two-factor")
public ResponseMessage<String> testTowFactor() {
return ResponseMessage.ok();
}
}

View File

@@ -0,0 +1,39 @@
package org.hswebframework.web.authorization.full.controller;
import org.hswebframework.web.authorization.twofactor.TwoFactorValidator;
import org.hswebframework.web.authorization.twofactor.TwoFactorValidatorProvider;
import org.springframework.stereotype.Component;
/**
* @author zhouhao
* @since 3.0.4
*/
@Component
public class TestTwoFactorValidatorProvider implements TwoFactorValidatorProvider {
@Override
public String getProvider() {
return "test";
}
@Override
public TwoFactorValidator createTwoFactorValidator(String userId, String operation) {
return new TwoFactorValidator() {
boolean success = false;
@Override
public String getProvider() {
return "test";
}
@Override
public boolean verify(String code, long timeout) {
return success = code.equalsIgnoreCase("test");
}
@Override
public boolean expired() {
return !success;
}
};
}
}

View File

@@ -1,59 +1,60 @@
spring:
aop:
auto: true
proxy-target-class: true
datasource:
url : jdbc:h2:mem:example-oauth2-client
username : sa
password :
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name : org.h2.Driver
cache:
type: simple
aop:
auto: true
proxy-target-class: true
datasource:
url: jdbc:h2:mem:example-oauth2-client
username: sa
password:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.h2.Driver
cache:
type: simple
hsweb:
app:
name: hsweb-oauth2 客户端示例
version: 3.0.0
authorize:
allows:
users:
admin: "**.TestController.*"
users:
admin:
name: 超级管理员
username: admin
password: admin
roles: #用户的角色
- id: admin
name: 管理员
- id: user
name: 用户
permissions-simple:
test: query,get
permissions:
- id: user-manager
actions: query,get,update,delete
dataAccesses:
- action: query
type: DENY_FIELDS
fields:
- password
- salt
- id: test
actions: query,add,update
dataAccesses:
- action: query
type: DENY_FIELDS
fields:
- password
- action: update
type: DENY_FIELDS
fields:
- name
- action: add
type: DENY_FIELDS
fields:
- id
app:
name: hsweb-oauth2 客户端示例
version: 3.0.0
authorize:
allows:
users:
admin: "**.TestController.*"
two-factor:
enable: true
users:
admin:
name: 超级管理员
username: admin
password: admin
roles: #用户的角色
- id: admin
name: 管理员
- id: user
name: 用户
permissions-simple:
test: query,get
permissions:
- id: user-manager
actions: query,get,update,delete
dataAccesses:
- action: query
type: DENY_FIELDS
fields:
- password
- salt
- id: test
actions: query,add,update
dataAccesses:
- action: query
type: DENY_FIELDS
fields:
- password
- action: update
type: DENY_FIELDS
fields:
- name
- action: add
type: DENY_FIELDS
fields:
- id
server:
port: 8808