当前位置: 首页 > 工具软件 > kisso > 使用案例 >

SpringBoot实战(十二)之集成kisso

端木宏才
2023-12-01

关于kisso介绍,大家可以参考官方文档或者是我的博客:https://www.cnblogs.com/youcong/p/9794735.html

一、导入maven依赖

     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.baomidou</groupId>
<artifactId>kisso_springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>kisso_springboot</name>
<url>http://maven.apache.org</url>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>kisso</artifactId>
        <version>3.7.0</version>
    </dependency>
</dependencies>

二、编写启动类

package com.baomidou.kisso;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

private static final Logger logger = LoggerFactory.getLogger(Application.class);

/**
 * <p>
 * 1、启动执行<br>
 * 2、访问 http://localhost:8080/token 提示登录<br>
 * 3、登录 访问 http://localhost:8080/login 成功再去 2 步骤查看<br>
 * </p>
 * <p>
 * 退出登录:http://localhost:8080/logout
 * </p>
 */
public static void main(String[] args) {
    SpringApplication application = new SpringApplication(Application.class);
    application.run(args);
    logger.info("kisso start!");
}

}

三、编写Controller

package com.baomidou.kisso;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.kisso.security.token.SSOToken;

@Controller
public class SampleController {

@Autowired
protected HttpServletRequest request;
@Autowired
protected HttpServletResponse response;

@ResponseBody
@RequestMapping("/")
public String home() {
    return "Hello Kisso!";
}

// 授权登录
@ResponseBody
@RequestMapping("/login")
public String login() {
    // 设置登录 COOKIE
    SSOHelper.setCookie(request, response, SSOToken.create().setIp(request).setId(1000).setIssuer("kisso"), false);
    return "login success!";
}

// 查看登录信息
@ResponseBody
@RequestMapping("/token")
public String token() {
    String msg = "暂未登录";
    SSOToken ssoToken = SSOHelper.attrToken(request);
    if (null != ssoToken) {
        msg = "登录信息 ip=" + ssoToken.getIp();
        msg += ", id=" + ssoToken.getId();
        msg += ", issuer=" + ssoToken.getIssuer();
    }
    return msg;
}

// 退出登录
@ResponseBody
@RequestMapping("/logout")
public String logout() {
    SSOHelper.clearLogin(request, response);
    return "Logout Kisso!";
}

}

四、编写WebConfig类

package com.baomidou.kisso;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.baomidou.kisso.web.interceptor.SSOSpringInterceptor;

@ControllerAdvice
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void addInterceptors(InterceptorRegistry registry) {
    // kisso 拦截器配置
    registry.addInterceptor(new SSOSpringInterceptor()).addPathPatterns("/**").excludePathPatterns("/login");
}

}

五、编写application.yml配置文件

server:

port: 8080

单点信息配置,全部可无

kisso:
config:

signkey: C691d971EJ3H376G81   # 对称签名密钥
cookieName: kisso             # COOKIE 名称

六、启动项目并测试

按照如下流程测试:

1.未登陆时直接访问

2.登陆

3.登陆后直接访问

  1. 退出
 类似资料: