ホームページ Java &#&チュートリアル Spring Boot による Spring セキュリティの使用法を共有して CAS の例を統合する

Spring Boot による Spring セキュリティの使用法を共有して CAS の例を統合する

May 27, 2017 am 10:16 AM

この記事では、Spring セキュリティを使用して CAS を統合するための Spring Boot の詳細な説明を主に紹介します。興味のある方は参照してください。

1. Maven プロジェクトを作成します。 - cas

2. 依存関係を追加します

プロジェクトを作成したら、pom.
xml
を開き、次の内容をpom.xmlに追加します:

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.4.3.RELEASE</version> 
  </parent> 
  <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <java.version>1.8</java.version> 
  </properties> 
  <dependencies> 
    <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter</artifactId> 
    </dependency> 
    <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
    <!-- security starter Poms --> 
    <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-security</artifactId> 
    </dependency> 
    <!-- security 对CAS支持 --> 
    <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-cas</artifactId> 
    </dependency> 
    <!-- security taglibs --> 
    <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-taglibs</artifactId> 
    </dependency> 
    <!-- 热加载 --> 
    <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-devtools</artifactId> 
      <optional>true</optional> 
    </dependency> 
    <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-configuration-processor</artifactId> 
      <optional>true</optional> 
    </dependency> 
  </dependencies> 
  <build> 
    <plugins> 
      <plugin> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
    </plugins> 
  </build>
ログイン後にコピー

3. application.propertiesを作成します

application.properties ファイルに次の内容を追加します:

#CAS服务地址 
cas.server.host.url=http://localhost:8081/cas 
#CAS服务登录地址 
cas.server.host.login_url=${cas.server.host.url}/login 
#CAS服务登出地址 
cas.server.host.logout_url=${cas.server.host.url}/logout?service=${app.server.host.url} 
#应用访问地址 
app.server.host.url=http://localhost:8080 
#应用登录地址 
app.login.url=/login 
#应用登出地址 
app.logout.url=/logout
ログイン後にコピー

4. 入口スタートアップ クラス (MainConfig) を作成します。

入口スタートアップ クラス MainConfig を作成します。完全なコードは次のとおりです。セキュリティ構成クラス (SecurityConfig)

セキュリティ構成クラス SecurityConfig を作成します。 完全なコードは次のとおりです:

package com.chengli.springboot; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.security.access.prepost.PreAuthorize; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 
 
@RestController 
@SpringBootApplication 
public class MainConfig { 
  public static void main(String[] args) { 
    SpringApplication.run(MainConfig.class, args); 
  } 
 
  @RequestMapping("/") 
  public String index() { 
    return "访问了首页哦"; 
  } 
 
  @RequestMapping("/hello") 
  public String hello() { 
    return "不验证哦"; 
  } 
 
  @PreAuthorize("hasAuthority(&#39;TEST&#39;)")//有TEST权限的才能访问 
  @RequestMapping("/security") 
  public String security() { 
    return "hello world security"; 
  } 
 
  @PreAuthorize("hasAuthority(&#39;ADMIN&#39;)")//必须要有ADMIN权限的才能访问 
  @RequestMapping("/authorize") 
  public String authorize() { 
    return "有权限访问"; 
  } 
   
  /**这里注意的是,TEST与ADMIN只是权限编码,可以自己定义一套规则,根据实际情况即可*/ 
}
ログイン後にコピー

(1) を挿入するために使用されるユーザー定義クラス

。便利に使用するためにプロパティファイルで指定されたコンテンツをここに挿入しなくても問題ありません。現在の Spring 環境を取得できます。コードは次のとおりです。

package com.chengli.springboot.security; 
 
import org.jasig.cas.client.session.SingleSignOutFilter; 
import org.jasig.cas.client.validation.Cas20ServiceTicketValidator; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.security.cas.ServiceProperties; 
import org.springframework.security.cas.authentication.CasAssertionAuthenticationToken; 
import org.springframework.security.cas.authentication.CasAuthenticationProvider; 
import org.springframework.security.cas.web.CasAuthenticationEntryPoint; 
import org.springframework.security.cas.web.CasAuthenticationFilter; 
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 
import org.springframework.security.core.userdetails.AuthenticationUserDetailsService; 
import org.springframework.security.web.authentication.logout.LogoutFilter; 
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler; 
 
import com.chengli.springboot.custom.CustomUserDetailsService; 
import com.chengli.springboot.properties.CasProperties; 
 
@Configuration 
@EnableWebSecurity //启用web权限 
@EnableGlobalMethodSecurity(prePostEnabled = true) //启用方法验证 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 
  @Autowired 
  private CasProperties casProperties; 
   
  /**定义认证用户信息获取来源,密码校验规则等*/ 
  @Override 
  protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
    super.configure(auth); 
    auth.authenticationProvider(casAuthenticationProvider()); 
    //inMemoryAuthentication 从内存中获取 
    //auth.inMemoryAuthentication().withUser("chengli").password("123456").roles("USER") 
    //.and().withUser("admin").password("123456").roles("ADMIN"); 
     
    //jdbcAuthentication从数据库中获取,但是默认是以security提供的表结构 
    //usersByUsernameQuery 指定查询用户SQL 
    //authoritiesByUsernameQuery 指定查询权限SQL 
    //auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery(query).authoritiesByUsernameQuery(query); 
     
    //注入userDetailsService,需要实现userDetailsService接口 
    //auth.userDetailsService(userDetailsService); 
  } 
   
  /**定义安全策略*/ 
  @Override 
  protected void configure(HttpSecurity http) throws Exception { 
    http.authorizeRequests()//配置安全策略 
      //.antMatchers("/","/hello").permitAll()//定义/请求不需要验证 
      .anyRequest().authenticated()//其余的所有请求都需要验证 
      .and() 
    .logout() 
      .permitAll()//定义logout不需要验证 
      .and() 
    .formLogin();//使用form表单登录 
     
    http.exceptionHandling().authenticationEntryPoint(casAuthenticationEntryPoint()) 
      .and() 
      .addFilter(casAuthenticationFilter()) 
      .addFilterBefore(casLogoutFilter(), LogoutFilter.class) 
      .addFilterBefore(singleSignOutFilter(), CasAuthenticationFilter.class); 
     
    //http.csrf().disable(); //禁用CSRF 
  } 
   
  /**认证的入口*/ 
  @Bean 
  public CasAuthenticationEntryPoint casAuthenticationEntryPoint() { 
    CasAuthenticationEntryPoint casAuthenticationEntryPoint = new CasAuthenticationEntryPoint(); 
    casAuthenticationEntryPoint.setLoginUrl(casProperties.getCasServerLoginUrl()); 
    casAuthenticationEntryPoint.setServiceProperties(serviceProperties()); 
    return casAuthenticationEntryPoint; 
  } 
   
  /**指定service相关信息*/ 
  @Bean 
  public ServiceProperties serviceProperties() { 
    ServiceProperties serviceProperties = new ServiceProperties(); 
    serviceProperties.setService(casProperties.getAppServerUrl() + casProperties.getAppLoginUrl()); 
    serviceProperties.setAuthenticateAllArtifacts(true); 
    return serviceProperties; 
  } 
   
  /**CAS认证过滤器*/ 
  @Bean 
  public CasAuthenticationFilter casAuthenticationFilter() throws Exception { 
    CasAuthenticationFilter casAuthenticationFilter = new CasAuthenticationFilter(); 
    casAuthenticationFilter.setAuthenticationManager(authenticationManager()); 
    casAuthenticationFilter.setFilterProcessesUrl(casProperties.getAppLoginUrl()); 
    return casAuthenticationFilter; 
  } 
   
  /**cas 认证 Provider*/ 
  @Bean 
  public CasAuthenticationProvider casAuthenticationProvider() { 
    CasAuthenticationProvider casAuthenticationProvider = new CasAuthenticationProvider(); 
    casAuthenticationProvider.setAuthenticationUserDetailsService(customUserDetailsService()); 
    //casAuthenticationProvider.setUserDetailsService(customUserDetailsService()); //这里只是接口类型,实现的接口不一样,都可以的。 
    casAuthenticationProvider.setServiceProperties(serviceProperties()); 
    casAuthenticationProvider.setTicketValidator(cas20ServiceTicketValidator()); 
    casAuthenticationProvider.setKey("casAuthenticationProviderKey"); 
    return casAuthenticationProvider; 
  } 
   
  /*@Bean 
  public UserDetailsService customUserDetailsService(){ 
    return new CustomUserDetailsService(); 
  }*/ 
   
  /**用户自定义的AuthenticationUserDetailsService*/ 
  @Bean 
  public AuthenticationUserDetailsService<CasAssertionAuthenticationToken> customUserDetailsService(){ 
    return new CustomUserDetailsService(); 
  } 
   
  @Bean 
  public Cas20ServiceTicketValidator cas20ServiceTicketValidator() { 
    return new Cas20ServiceTicketValidator(casProperties.getCasServerUrl()); 
  } 
   
  /**单点登出过滤器*/ 
  @Bean 
  public SingleSignOutFilter singleSignOutFilter() { 
    SingleSignOutFilter singleSignOutFilter = new SingleSignOutFilter(); 
    singleSignOutFilter.setCasServerUrlPrefix(casProperties.getCasServerUrl()); 
    singleSignOutFilter.setIgnoreInitConfiguration(true); 
    return singleSignOutFilter; 
  } 
   
  /**请求单点退出过滤器*/ 
  @Bean 
  public LogoutFilter casLogoutFilter() { 
    LogoutFilter logoutFilter = new LogoutFilter(casProperties.getCasServerLogoutUrl(), new SecurityContextLogoutHandler()); 
    logoutFilter.setFilterProcessesUrl(casProperties.getAppLogoutUrl()); 
    return logoutFilter; 
  } 
}
ログイン後にコピー

(2) CustomUserDetailsS​​ervice クラスを定義します。コードは次のとおりです。

package com.chengli.springboot.properties; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.stereotype.Component; 
 
/** 
 * CAS的配置参数 
 * @author ChengLi 
 */ 
@Component 
public class CasProperties { 
  @Value("${cas.server.host.url}") 
  private String casServerUrl; 
 
  @Value("${cas.server.host.login_url}") 
  private String casServerLoginUrl; 
 
  @Value("${cas.server.host.logout_url}") 
  private String casServerLogoutUrl; 
 
  @Value("${app.server.host.url}") 
  private String appServerUrl; 
 
  @Value("${app.login.url}") 
  private String appLoginUrl; 
 
  @Value("${app.logout.url}") 
  private String appLogoutUrl; 
......省略 getters setters 方法 
}
ログイン後にコピー
(3) 現在ログインしているユーザーの権限情報をロードするために使用される AuthorityInfo クラスを定義し、GrantedAuthority インターフェイスを実装します。コードは次のとおりです:
package com.chengli.springboot.custom; 
 
import java.util.HashSet; 
import java.util.Set; 
 
import org.springframework.security.cas.authentication.CasAssertionAuthenticationToken; 
import org.springframework.security.core.userdetails.AuthenticationUserDetailsService; 
import org.springframework.security.core.userdetails.UserDetails; 
import org.springframework.security.core.userdetails.UsernameNotFoundException; 
 
/** 
 * 用于加载用户信息 实现UserDetailsService接口,或者实现AuthenticationUserDetailsService接口 
 * @author ChengLi 
 * 
 */ 
public class CustomUserDetailsService /* 
  //实现UserDetailsService接口,实现loadUserByUsername方法 
  implements UserDetailsService { 
  @Override 
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 
    System.out.println("当前的用户名是:"+username); 
    //这里我为了方便,就直接返回一个用户信息,实际当中这里修改为查询数据库或者调用服务什么的来获取用户信息 
    UserInfo userInfo = new UserInfo(); 
    userInfo.setUsername("admin"); 
    userInfo.setName("admin"); 
    Set<AuthorityInfo> authorities = new HashSet<AuthorityInfo>(); 
    AuthorityInfo authorityInfo = new AuthorityInfo("TEST"); 
    authorities.add(authorityInfo); 
    userInfo.setAuthorities(authorities); 
    return userInfo; 
  }*/ 
   
   
  //实现AuthenticationUserDetailsService,实现loadUserDetails方法 
  implements AuthenticationUserDetailsService<CasAssertionAuthenticationToken> { 
 
  @Override 
  public UserDetails loadUserDetails(CasAssertionAuthenticationToken token) throws UsernameNotFoundException { 
    System.out.println("当前的用户名是:"+token.getName()); 
    /*这里我为了方便,就直接返回一个用户信息,实际当中这里修改为查询数据库或者调用服务什么的来获取用户信息*/ 
    UserInfo userInfo = new UserInfo(); 
    userInfo.setUsername("admin"); 
    userInfo.setName("admin"); 
    Set<AuthorityInfo> authorities = new HashSet<AuthorityInfo>(); 
    AuthorityInfo authorityInfo = new AuthorityInfo("TEST"); 
    authorities.add(authorityInfo); 
    userInfo.setAuthorities(authorities); 
    return userInfo; 
  } 
 
}
ログイン後にコピー

(4) ロードするために使用される UserInfo クラスを定義します現在のユーザー情報を取得し、UserDetails インターフェイスを実装します。 コードは次のとおりです。

package com.chengli.springboot.custom; 
 
import org.springframework.security.core.GrantedAuthority; 
 
/** 
 * 权限信息 
 * 
 * @author ChengLi 
 * 
 */ 
public class AuthorityInfo implements GrantedAuthority { 
  private static final long serialVersionUID = -175781100474818800L; 
 
  /** 
   * 权限CODE 
   */ 
  private String authority; 
 
  public AuthorityInfo(String authority) { 
    this.authority = authority; 
  } 
 
  @Override 
  public String getAuthority() { 
    return authority; 
  } 
 
  public void setAuthority(String authority) { 
    this.authority = authority; 
  } 
 
}
ログイン後にコピー

これで基本的には完了です。 CAS サーバーを実行し、上記の application.properties ファイルのアドレスを実際のアドレスに変更して実行します。

【関連する推奨事項】

1.

Bootstrap Table の使用体験の概要

2.

ビジネス層での mysql の読み書き分離を実装するための詳細な説明

3. MySQL データベースと JPA インスタンスを Spring Boot Share に追加します

4. Spring Boot を使用して Restful プログラムを開発するためのサンプル チュートリアルを共有します

以上がSpring Boot による Spring セキュリティの使用法を共有して CAS の例を統合するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

Spring Security 6: cors() は非推奨となり、削除対象としてマークされています Spring Security 6: cors() は非推奨となり、削除対象としてマークされています Feb 10, 2024 pm 11:45 PM

次のコードがあります: publicSecurityFilterChainsecurityFilterChain(HttpSecurityhttp)throwsException{returnhttp.httpBasic().disable().cors().and().csrf().disable().authorizeHttpRequests().requestMatchers("

Java を使用して Spring Security SAML ベースのシングル サインオン システムを開発する方法 Java を使用して Spring Security SAML ベースのシングル サインオン システムを開発する方法 Sep 22, 2023 am 08:49 AM

Java を使用して SpringSecuritySAML に基づくシングル サインオン システムを開発する方法 はじめに: インターネットの急速な発展に伴い、ますます多くのアプリケーションが開発されています。これらのアプリケーションでは、ユーザー ログインは最も一般的な機能の 1 つです。ただし、エンタープライズ レベルのアプリケーションの場合、ユーザーは複数のシステムにログインする必要があるため、ユーザーのログイン エクスペリエンスが非常に低下します。この問題を解決するために登場したのがシングルサインオンシステム(SingleSign-On、以下SSO)です。単純

Spring Boot + MyBatis + Atomikos + MySQL (ソースコード付き) Spring Boot + MyBatis + Atomikos + MySQL (ソースコード付き) Aug 15, 2023 pm 04:12 PM

実際のプロジェクトでは、分散トランザクションを避けるように努めます。ただし、場合によってはサービスを分割する必要があり、分散トランザクションの問題が発生することがあります。同時に、分散取引についても面接で質問されるので、このケースで練習し、面接で 123 について話すことができます。

Spring Boot を使用してビッグデータ処理アプリケーションを構築する方法 Spring Boot を使用してビッグデータ処理アプリケーションを構築する方法 Jun 23, 2023 am 09:07 AM

ビッグデータ時代の到来により、ますます多くの企業がビッグデータの価値を理解し、認識し、ビジネスに活用し始めています。それに伴う問題は、この大規模なデータ フローをどのように処理するかです。この場合、ビッグ データ処理アプリケーションは、すべての企業が検討しなければならないものになっています。開発者にとっては、SpringBoot を使用して効率的なビッグデータ処理アプリケーションを構築する方法も非常に重要な問題です。 SpringBoot は非常に人気のある Java フレームワークです。

Spring Boot を通じて多言語サポートと国際アプリケーションを実現 Spring Boot を通じて多言語サポートと国際アプリケーションを実現 Jun 23, 2023 am 09:09 AM

グローバリゼーションの進展に伴い、多言語サポートや国際化機能を提供する必要のある Web サイトやアプリケーションがますます増えています。開発者にとって、これらの機能を実装することは、言語翻訳、日付、時刻、通貨の形式など、多くの側面を考慮する必要があるため、簡単な作業ではありません。ただし、SpringBoot フレームワークを使用すると、多言語サポートと国際アプリケーションを簡単に実装できます。まず、SpringBoot が提供する LocaleResolver インターフェースについて理解しましょう。ロック

Spring Boot と NoSQL データベースの統合と使用 Spring Boot と NoSQL データベースの統合と使用 Jun 22, 2023 pm 10:34 PM

インターネットの発展に伴い、ビッグデータ分析とリアルタイム情報処理が企業にとって重要なニーズとなっています。このようなニーズを満たすために、従来のリレーショナル データベースはビジネスやテクノロジー開発のニーズを満たせなくなりました。代わりに、NoSQL データベースを使用することが重要なオプションになっています。この記事では、最新のアプリケーションの開発と展開を可能にする、NoSQL データベースと統合された SpringBoot の使用について説明します。 NoSQL データベースとは何ですか? NoSQL は SQL だけではありません

Spring Boot と MyBatis Plus に基づいて ORM マッピングを実装する Spring Boot と MyBatis Plus に基づいて ORM マッピングを実装する Jun 22, 2023 pm 09:27 PM

Java Web アプリケーションの開発プロセスでは、ORM (Object-RelationalMapping) マッピング テクノロジを使用してデータベース内のリレーショナル データを Java オブジェクトにマッピングし、開発者がデータにアクセスして操作するのを容易にします。 SpringBoot は、最も人気のある Java Web 開発フレームワークの 1 つとして、MyBatis を統合する方法を提供しています。MyBatisPlus は、MyBatis に基づいて拡張された ORM フレームワークです。

Spring Bootのタスクスケジューリングとスケジュールされたタスクの実装方法 Spring Bootのタスクスケジューリングとスケジュールされたタスクの実装方法 Jun 22, 2023 pm 11:58 PM

SpringBoot は非常に人気のある Java 開発フレームワークであり、迅速な開発という利点があるだけでなく、多くの実用的な機能が組み込まれており、その中でもタスク スケジューリングとスケジュールされたタスクはよく使用される機能の 1 つです。この記事では、SpringBoot のタスクのスケジューリングとタイミング タスクの実装方法について説明します。 1. SpringBoot タスク スケジューリングの概要 SpringBoot タスク スケジューリング (TaskScheduling) は、特定の時点または特定の条件下でいくつかの特別なタスクを実行することを指します。

See all articles