入門
Springsecurity Springsecurityは春のプロジェクトのためのセキュリティフレームワークであり、またSpringbootの基本的なセキュリティモジュールのデフォルトの技術選択である。それは、強力なWebセキュリティ制御を実現することができます。セキュリティ制御のために、我々は唯一の導入する必要があるの主なカテゴリSpring-をスターターセキュリティモジュール
。
@WebSecurityConfigurerAdapter:カスタムセキュリティ戦略
@AuthenticationManagerBuider:カスタム認証戦略
@EnableWebSecurityはWebSecurityモードをオンにし、@ Enable XXXは特定の機能を
オンにしますSpringsecurityの2つの主な目標は、「認証」と「承認」(アクセス制御)です。
概念は相互運用可能であるだけではありません。Springsecurityで。
準備、依存関係のインポート
<!--引入thymeleaf依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- security依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
いくつかの単純なページディレクトリ構造
ページの効果
独自の構成クラスを作成するSpringbootは、多くの統合を支援します。クラスを作成するだけで、WebSecurityConfigurerAdapterを継承します。アノテーション@EnableWebSecurityを追加します。
package com.jj.config;
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.WebSecurityConfiguration;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class Securityconfig extends WebSecurityConfigurerAdapter {
// 重写
@Override
protected void configure(HttpSecurity http) throws Exception {
// 设置首页所有人可以访问
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
// 开启登录 ,没有权限退到登录页
http.formLogin();
}
}
その結果、 Springのaopアスペクト指向に相当する
権限がないため、クリックするとログインページに戻ります
。
ログインアカウントのパスワード権限を設定する
// 重写登录的账号密码
// 可以是数据库的,我没有连接数据库,就用的内存的
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
// 设置fjj 可以看 vip2 ,vip3
.withUser("fengjiaojaio").password("123456").roles("vip2","vip3")
// 设置憨憨 只能看vip1
.and() .withUser("憨憨").password("285").roles("vip1");
}
バージョンの問題でエラーが報告され、パスワードが安全でないことが示されます。多分それはみんなのパスワードのためです、ahhahahahaha
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
at org.springframework.security.crypto.password.DelegatingPasswordEncoder$UnmappedIdPasswordEncoder.matches(DelegatingPasswordEncoder.java:254) ~[spring-security-core-5.4.2.jar:5.4.2]
at org.springframework.security.crypto.password.DelegatingPasswordEncoder.matches(DelegatingPasswordEncoder.java:202) ~[spring-security-core-5.4.2.jar:5.4.2]
**解決策1:パスワード暗号化コードを設定したくない場合は、バージョンを2.1.Xに減らすことができます**
**解決策2:パスワード暗号化を追加します**
// 重写登录的账号密码
// 可以是数据库的,我没有连接数据库,就用的内存的
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
// 设置fjj 可以看 vip2 ,vip3
.withUser("fengjiaojaio").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
// 设置憨憨 只能看vip1
.and() .withUser("憨憨").password(new BCryptPasswordEncoder().encode("285")).roles("vip1");
}
効果
は許可がないことを示します!!
データベースに接続すると、公式ドキュメントにデータソースが挿入されます。!
ログイン後にCookieが発生するため、ログインを解除するにはオンにする必要があります。さらに、この文を入力して、
ホームページにログアウトボタンを書き込みます。
このマッパーはSpringセキュリティによって作成され
てい
ます。クリックしてソースコードをダウンロードすると、次のように表示されます。コメント
効果ログインインターフェイスにジャンプしたくない場合
は、わかりやすいプロンプト
が表示されます。指定されたURL
**
関数にジャンプすることもできます。!