依赖 这里我们直接使用springboot内部集成的版本
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
</dependency>
添加配置类
@Configuration
@EnableSwagger2
@EnableKnife4j
public class Knife4jConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.doria.saToken"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.description("SaToken-Demo Document")
.contact(new Contact("哈哈哈", "https://github.com.lenve", "1916008067@qq.com"))
.version("v1.1.0")
.title("API测试文档")
.build();
}
}
使用
@ApiModel(value = "用户信息")
public class SysUser {
@ApiModelProperty(value = "id")
private Long id;
@ApiModelProperty(value = "名称")
private String name;
@ApiModelProperty(value = "性别")
private String gender;
@ApiModelProperty(value = "年龄")
private Integer age;
public SysUser(Long id, String name) {
this.id = id;
this.name = name;
}
}
@Api(tags = "测试文档")
@RestController
@RequestMapping(value = "/test")
public class TestController {
@ApiOperation(value = "登录")
@PostMapping(value = "/login")
public BizResult<SysUser> login(@RequestBody @Valid LoginDto loginDto) {
if (!loginDto.getUsername().equals(UserConfig.username)) {
return fail();
}
if (!loginDto.getPassword().equals(UserConfig.password)) {
return fail();
}
StpUtil.login(556L);
SysUser user = new SysUser(556L, "法外狂徒张三", "man", 18);
return success(user);
}
@ApiOperation(value = "退出登录")
@PutMapping(value = "/logout")
public BizResult<String> logout() {
StpUtil.logout();
return success();
}
@ApiOperation(value = "获取用户信息")
@GetMapping(value = "/info")
public BizResult<UserInfo> info() {
if (!StpUtil.isLogin())
throw new NotLoginException("没登陆", "user not login", "un login");
return success(new UserInfo(556L, "法外狂徒张三", "woman", 19));
}
@ApiOperation(value = "查询当前会话是否登录")
@GetMapping(value = "/isLogin")
public BizResult<String> isLogin() {
return success("当前会话是否登录:" + StpUtil.isLogin());
}
@ApiOperation(value = "获取当前用户userId")
@GetMapping(value = "/getUserId")
public BizResult<String> getUserId() {
return success("用户id为" + StpUtil.getLoginIdAsLong());
}
@ApiOperation(value = "获取当前用户权限")
@GetMapping(value = "getPermission")
public BizResult<Object> getUserPermission() {
return success(StpUtil.getPermissionList());
}
@ApiOperation(value = "获取当前用户角色")
@GetMapping(value = "getUserRole")
public BizResult<Object> getUserRole() {
return success(StpUtil.getRoleList());
}
@ApiOperation(value = "检查当前用户是否拥有角色")
@PostMapping(value = "/checkRole")
@ApiImplicitParam(value = "角色",name = "role",required = true)
public BizResult<Object> checkRole(@RequestParam String role) {
StpUtil.checkRole(role);
return success(StpUtil.hasRole(role));
}
@ApiOperation(value = "检查当前用户是否拥有权限")
@PostMapping(value = "/checkPermission")
@ApiImplicitParam(value = "权限",name = "permission",required = true)
public BizResult<Object> checkPermission(@RequestParam String permission) {
StpUtil.checkPermission(permission);
return success(StpUtil.hasPermission(permission));
}
@ApiOperation(value = "将指定用户踢下线")
@PutMapping(value = "/kickoutUser")
public BizResult<Object> kickoutUser(@RequestParam Long userId){
StpUtil.checkRole("admin");
SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
System.out.println(tokenInfo);
StpUtil.kickout(userId);
return success();
}
}
|