• SpringBoot实战(十二)之集成kisso


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

    一、导入maven依赖

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             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>
    </project>

    二、编写启动类

    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.登陆后直接访问

    4. 退出

     

  • 相关阅读:
    汇编中寄存器及其用处
    直接打印则需要调用PrintDocumnt.Print()方可打印,否按在对话框中点【打印】但不会有反应
    操作另一窗体的变量事件
    System.Windows.Forms.ListControl.SelectedValue.get 返回 null
    combox的selectedValue初始值注意事项
    vs2017中EF6.4无法导入到PM中,应使用EF6.2或6.1.1
    去除 Datetime的字段会自动赋默认值0001-1-1 0:00:00
    EF使用问题备忘
    EF中自定义连接字符串
    C#如何改变DataTable中的数据?
  • 原文地址:https://www.cnblogs.com/youcong/p/9794776.html
Copyright © 2020-2023  润新知