1.上一项目添加依赖
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.7</version>
</dependency>
2.创建配置文件
package com.jx.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
@EnableSwagger2WebMvc
@Configuration
public class Knife4jConfig {
@Bean(value = "defaultApi2")
public Docket defaultApi2() {
Contact contact = new Contact("jx","https://blog.csdn.net/qq_26440111?spm=1000.2115.3001.5343","1339237433@qq.com");
Docket docket=new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
.title("SprinBoot整合Shiro测试接口文档")
.contact(contact)
.description("# SprinBoot测试接口文档")
.termsOfServiceUrl("http://www.bilibili.com/")
.version("1.0")
.build())
.groupName("Shiro测试接口")
.select()
.apis(RequestHandlerSelectors.basePackage("com.jx.controller"))
.paths(PathSelectors.any())
.build();
return docket;
}
}
3.给Controller和方法增加接口注解
package com.jx.controller;
import com.jx.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.RequestParam;
@Api(tags="SpringBoot测试模块")
@Controller
public class HelloController {
@Autowired
private UserService userService;
@ApiOperation(value = "主页面")
@GetMapping({"/index"})
public String hello() {
return "index";
}
@ApiOperation(value = "登录请求")
@GetMapping("/login")
public String login(@RequestParam("username") String username,
@RequestParam("password") String password,
Model model) {
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
try {
subject.login(token);
return "index";
} catch (UnknownAccountException e) {
System.out.println("用户名不存在");
model.addAttribute("msg", "用户名不存在!");
return "login";
} catch (IncorrectCredentialsException e) {
System.out.println("密码错误");
model.addAttribute("msg", "密码错误!");
return "login";
}
}
@ApiOperation(value = "登录页")
@GetMapping("/toLogin")
public String toLogin() {
return "/login";
}
@ApiOperation(value = "VIP1页面")
@GetMapping("level1/{id}")
public String toMain1(@PathVariable("id") int id) {
return "views/level1/" + id;
}
@ApiOperation(value = "VIP2页面")
@GetMapping("level2/{id}")
public String toMain2(@PathVariable("id") int id) {
return "views/level2/" + id;
}
@ApiOperation(value = "VIP3页面")
@GetMapping("level3/{id}")
public String toMain3(@PathVariable("id") int id) {
return "views/level3/" + id;
}
@ApiOperation(value = "注销登录")
@GetMapping ("/logout")
public String logout() {
Subject currentUser = SecurityUtils.getSubject();
currentUser.getSession().removeAttribute("currentUser");
currentUser.logout();
return "/login";
}
}
4.访问http://localhost:8080/doc.html
5.主页
6.接口页面