zoukankan      html  css  js  c++  java
  • spring boot-整合CAS Client实现单点登陆验证

    依赖:

    <dependency>
    <groupId>net.unicon.cas</groupId>
    <artifactId>cas-client-autoconfig-support</artifactId>
    <version>1.4.0-GA</version>
    </dependency>

    增加配置文件:

    cas.server-url-prefix=http://127.0.0.1            地址前缀
    cas.server-login-url=http://127.0.0.1/login   认证地址
    cas.client-host-url=http://192.26.4.28:8080   认证成功跳转地址
    cas.validation-type=CAS    
    
    -----cas.validation-type目前支持3中方式:1、CAS;2、CAS3;3、SAML

    开启CAS Client支持

    @SpringBootApplication
    
    @ComponentScan(basePackages={"com.chhliu.emailservice"})
    @EnableCasClient // 开启CAS支持
    public class Application extends SpringBootServletInitializer{
     public static void main(String[] args) {
     SpringApplication.run(Application.class, args);
       
     }
    }

     

    测试方法:

    @Controller
    public class TestController {
    
        @Value(value = "${cas.server-url-prefix}")
        private String serverUrlPrefix = "";
    
        @Value(value = "${cas.client-host-url}")
        private String clientHostUrl = "";
    
        @GetMapping("user")
        @ResponseBody
        public String user(HttpServletRequest request) {
            Assertion assertion = (Assertion) request.getSession().getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION);
            String loginName = null;
            if (assertion != null) {
                AttributePrincipal principal = assertion.getPrincipal();
                loginName = principal.getName();
                System.out.println("访问者:" + loginName);
            }
            return "访问者:" + loginName;
        }
    
        @RequestMapping("/logout")
        public String logout(HttpSession session) {
            session.invalidate();
            return "redirect:" + serverUrlPrefix + "/logout?service=" + clientHostUrl + "/sso-client/user";
        }
    }

    5.1 登录:

    (1)打开浏览器访问:http://127.0.0.1:8888/sso-client/user
    (2)由于没有登录,重定向到:http://146.56.192.87:8080/cas/login?service=http://127.0.0.1:8888/sso-client/user
    (3)输入用户名密码,点击登录,成功之后跳转到http://127.0.0.1:8888/sso-client/user中

    5.2 退出:

    (4)访问:http://127.0.0.1:8888/sso-client/logout
    (5)cas完成退出,并跳转到http://127.0.0.1:8888/sso-client/user,由于该地址需要登录,仍会重定向到登录界面

  • 相关阅读:
    MVC3分页传2参
    C# 二进制存储图片到mssql(一)
    著名黑客组织[转]
    浅看C# md5加密
    google搜索技巧
    字符串编码转换 GBK utf8
    objectivec 中随机数的用法 (3种:arc4random() 、random()、CCRANDOM_0_1() )
    NSPredicate的用法
    Java关键字final、static使用总结()
    CGAffineTransform相关函数
  • 原文地址:https://www.cnblogs.com/chdchd/p/15015166.html
Copyright © 2011-2022 走看看