五、SpringBoot整合Knife4j

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()
                //这里指定Controller扫描包路径
                .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.接口页面

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值