Springboot+CAS單點登錄


一:安裝CAS

下載cas:https://github.com/apereo/cas

1.1 將cas並打成war包。放入一個干凈的tomcat中,啟動tomcat測試: http://localhost:8080/cas/login

  

1.2 默認賬號密碼:casuser     Mellon     我們可以在tomcat\webapps\cas\WEB-INF\deployerConfigContext.xml文件添加一個賬號密碼

  

1.3 修改tomcat端口為9080, 並將tomcat\webapps\cas\WEB-INF\cas.properties的server.name改為http://localhost:9080

1.4 去除https認證:

1.4.1 在tomcat\webapps\cas\WEB-INF\deployerConfigContext.xml文件
      的p:httpClient-ref="httpClient"后面添加p:requireSecure="false" 
1.4.2 把tomcat\webapps\cas\WEB-INF\spring-configuration的
      ticketGrantingTicketCookieGenerator.xml文件里面把p:cookieSecure="true"改為false;
      p:cookieMaxAge="-1"改為3600(-1是不保存cookie,3600秒是一個小時,保存登錄信息)
1.4.3 把tomcat\webapps\cas\WEB-INF\spring-configuration的
      warnCookieGenerator.xml的p:cookieSecure="true"改為false
      p:cookieMaxAge="-1"改為3600

  

1.5 配置單點登出: 將tomcat\webapps\cas\WEB-INF\cas-servlet.xml中${cas.logout.followServiceRedirects:false}括號里的值改為true

1.6 啟動測試:  輸入剛才配置的賬號密碼   wulei / wulei

  

二:配置數據源(CAS對接數據庫)

2.1 在tomcat\webapps\cas\WEB-INF\lib里添加 c3p0連接池   mysql驅動   cas的jdbc支持包

  

2.2 修改tomcat\webapps\cas\WEB-INF\deployerConfigContext.xml文件

2.2.1 注釋掉<entry key-ref="primaryAuthenticationHandler" value-ref="primaryPrincipalResolver" />;添加<entry key-ref="dbAuthHandler" value-ref="primaryPrincipalResolver"/>

  

2.2.2  添加數據源   <bean id="dataSource"    添加加密方式 <bean id="passwordEncoder"     添加sql語句  <bean id="dbAuthHandler"

    <!-- 第一個bean -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
              p:driverClass="com.mysql.jdbc.Driver"  
              p:jdbcUrl="jdbc:mysql://127.0.0.1:3306/youfanshop?characterEncoding=utf8"  
              p:user="root"  
              p:password="root" /> 
    <!-- 第二個bean          
    <bean id="passwordEncoder" 
              class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder"  
              c:encodingAlgorithm="MD5"  
              p:characterEncoding="UTF-8" /> -->
    <!-- 第三個bean 
    <bean id="dbAuthHandler"  
          class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler"  
          p:dataSource-ref="dataSource"  
          p:sql="select passwordencrypt from user where name  = ?"  
          我們密碼用明文, 所以把加密方式注釋掉, 
          p:passwordEncoder-ref="passwordEncoder"
          />  -->
    <bean id="dbAuthHandler"  
          class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler"  
          p:dataSource-ref="dataSource"  
          p:sql="select passwordencrypt from user where name  = ?" />

  

2.3 重啟測試(此時就能用數據庫的賬號密碼登錄了)

   

三:springBoot客戶端

3.1 導包

    <parent> 
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.13.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    
    <dependencies>
        <!--web場景啟動器,包含 Tomcat 和 spring-mvc restful  aop jackjson支持。 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- CAS依賴包 -->
        <dependency>
            <groupId>net.unicon.cas</groupId>
            <artifactId>cas-client-autoconfig-support</artifactId>
            <version>1.5.0-GA</version>
        </dependency>
    </dependencies>

3.2 application.properties

server.port=8081

cas.server-url-prefix=http\://127.0.0.1\:9080/cas
cas.server-login-url=http\://127.0.0.1\:9080/cas/login
cas.client-host-url=http\://127.0.0.1\:8081
cas.validation-type=CAS

3.3 配置類

import net.unicon.cas.client.configuration.CasClientConfigurerAdapter;
import net.unicon.cas.client.configuration.EnableCasClient;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableCasClient
public class CasConfigure extends CasClientConfigurerAdapter {
@Override
public void configureAuthenticationFilter(FilterRegistrationBean authenticationFilter) {
    super.configureAuthenticationFilter(authenticationFilter);
        authenticationFilter.getInitParameters().put("authenticationRedirectStrategyClass","com.patterncat.CustomAuthRedirectStrategy");
    }
}

3.4 控制器

@RestController
public class IndexController {
    
    @RequestMapping("/login")
    public String auth() {
        return "login success";
    }
}

3.5 主函數

@SpringBootApplication
public class Application {

     private static Logger log = Logger.getLogger(Application.class);
     
     public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
            log.info("SpringBoot Start Success");
        }
}

測試:  瀏覽器輸入   127.0.0.1:8081/login之前會先跳轉到CAS的登陸頁面,登錄成功之后才會進入Controller。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM