zoukankan      html  css  js  c++  java
  • springboot10

    注意:casServerLoginUrl和casServerUrlPrefix必须使用域名,且域名要和证书中的“名字与姓氏”完全相同,没有域名的可以配置本地hosts做映射 
    我的hosts配置是这样: 
    这里写图片描述 
    这里用的一台机器,两个域名均指向了本机,有域名的童鞋及土豪无视

    c) spring-mvc.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans     
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            ">
    
        <mvc:annotation-driven />
        <context:component-scan base-package="com.castest.*" />
    
    </beans>

    d) 测试Controller

    package com.castest.cas;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class IndexController {
    
    
        @RequestMapping("/test")
        @ResponseBody
        public String index(HttpServletRequest request, HttpServletResponse response) {
            String result = "execute test method</br>";
            result +=   "sessionId                  :   "+request.getSession().getId()  +"</br>";
            result +=   "request.getRemoteUser()    :   " + request.getRemoteUser()     +"</br>";
            result +=   "request.getUserPrincipal() :   " + request.getUserPrincipal()  +"</br>";
            return result;
        }
    
        @RequestMapping(value={"/","/index"})
        @ResponseBody
        public String error(HttpServletRequest request, HttpServletResponse response) {
            String result = "execute index method</br>";
            result +=   "sessionId                  :   "+request.getSession().getId()  +"</br>";
            result +=   "request.getRemoteUser()    :   " + request.getRemoteUser()     +"</br>";
            result +=   "request.getUserPrincipal() :   " + request.getUserPrincipal()  +"</br>";
            return result;
        }
    
    
    }

    e) 测试结果

    1、 
    请求:http://www.zrk1000.com:8081/cas_client/index

    这里写图片描述

    结果:web.xml中只对/test做登陆拦截,/index未跳转登陆正常

    2、 
    请求:http://www.zrk1000.com:8081/cas_client/test 
    –> https://cas.castest.com:8443/cas/login?service=http%3A%2F%2Fwww.zrk1000.com%3A8081%2Fcas_client%2Ftest 
    这里写图片描述
    被拦截重定向到了cas-server的默认登陆页面;server参数为原请求URL,认证成功后会重定向到此地址。 
    cas默认用户名为:casuser 默认密码:Mellon (cas-server的WEB-INF/deployerConfigContext.xml中) 
    –>http://www.zrk1000.com:8081/cas_client/test

    这里写图片描述

    结果:web.xml中只对/test做登陆拦截,/index不跳转;登陆正常,并且登陆成功后获取到了用户信息

    2、基于spring boot配置

    使用spring boot创建web项目没有web.xml,只能使用java代码的方式添加filter和listener,原理和web.xml一样,这里做了简单的配置, 
    github上源码:https://github.com/zrk1000/cas_client_boothttps://github.com/zrk1000/cas_client_boot_demo 
    或者源码压缩包:cas_client_boot_test_demo.zip 
    其中cas_client_boot_demo.war依赖cas_client_boot.jar。 
    使用spring boot的项目cas这样配置:



    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    server.session.cookie.domain=.tjresearch.com
    server.session.cookie.path=/
     
     
    spring.cas.sign-out-filters=/logout
    spring.cas.auth-filters=/*
    spring.cas.validate-filters=/*
    spring.cas.request-wrapper-filters=/*
    spring.cas.assertion-filters=/*
     
     
    spring.cas.cas-server-login-url=http://sso.tjresearch.com:8080/cas/login
    spring.cas.cas-server-url-prefix=http://sso.tjresearch.com:8080/cas/
    spring.cas.redirect-after-validation=true
    spring.cas.use-session=true
    spring.cas.server-name=http://area.tjresearch.com:8805/newArea

     

    配置ajax跨域
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    @Configuration
    public class CorsConfig extends WebMvcConfigurerAdapter {
     
     
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**").allowedOrigins("http://area.tjresearch.com:8805").allowedMethods("GET""HEAD""POST""PUT""DELETE""OPTIONS")
                    .allowCredentials(true).maxAge(3600);
        }
     
    }

    ajax使用域名的形式,不能用Ip地址的形式,包括上面的配置文件。

     

    3 常见错误

    1、javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching cas.castest.com found
    原因:cas客户端使用的证书库中未找到相应的域名,也就是说客户端JDK导入的证书的域名与客户端访问的cas-server域名不同
    解决:检查JDK证书库中证书域名是否和cas-server的域名相同,保持一致即可
    
    2、javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    原因:证书未导入等原因
    解决:往jdk导入证书,使用IDE的小伙伴也要检查工具使用的jdk是否和你导入证书的jdk是同一个
  • 相关阅读:
    世界上最遥远的距离(泰戈尔)
    肩周炎?
    [转]C#基础概念二十五问
    [转]ASP.NET 2.0的缓存技术简介
    SQL Server 2005下的分页SQL
    [转]基本的缓存类操作封装(抽象类)
    GridView鼠标经过行变色
    推荐一个好用的.NET2.0下的发送EMail的库
    Data Access Application Block for .NET
    Pet Shop 4.0的缓存机制
  • 原文地址:https://www.cnblogs.com/huaobin/p/14909233.html
Copyright © 2011-2022 走看看