Springboot+CAS單點登陸

一:安裝CAS

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

1.1 將cas並打成war包。放入一個乾淨的tomcat中,啓動tomcat測試: http://localhost:8080/cas/loginmysql

  

1.2 默認帳號密碼:casuser     Mellon     咱們能夠在tomcat\webapps\cas\WEB-INF\deployerConfigContext.xml文件添加一個帳號密碼git

  

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

1.4 去除https認證:web

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}括號裏的值改成truespring

1.6 啓動測試:  輸入剛纔配置的帳號密碼   wulei / wuleisql

  

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

2.1 在tomcat\webapps\cas\WEB-INF\lib裏添加 c3p0鏈接池   mysql驅動   cas的jdbc支持包數據庫

  

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

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。

相關文章
相關標籤/搜索