SpringBoot-セキュリティ
一、春のセキュリティ
1.基本的な考え方
2つのセキュリティフレームワーク:shiro、Spring Security
Spring Securityは、Springプロジェクトのセキュリティフレームワークであり、SpringBootの基盤となるセキュリティモジュールのデフォルトのテクノロジ選択です。彼は強力なWebセキュリティ制御を実現できます。セキュリティ制御については、spring-boot-starter-securityモジュールを導入し、少量の構成を実行するだけで、強力なセキュリティ管理を実現できます。
- WebSecurityConfigurerAdapter:カスタムセキュリティ戦略
- AuthenticationManagerBuilder:カスタム認証戦略
- @EnableWebSecurity:WebSecurityモードをオンにします
アプリケーションがセキュリティ問題を処理する2つの主な領域は、「認証」と「承認」(アクセス制御)です。
- 「認証」(認証):主に、ユーザー名/パスワードを使用して件名を確認するプロセスを指します。
- 「承認」:サブジェクトがアプリケーションで特定の操作を実行できるかどうかを示します。
公式ドキュメント:https://docs.spring.io/spring-security/site/docs/5.4.2/reference/html5/#servlet-hello
この場合の完全なコード:SpringBootSecurity
2.依存関係を導入します
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
3.セキュリティ構成クラスを記述します
要求されたアクセスを制御する
@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");
//开启登录功能
//访问上述请求时,会来到登录页"/login",当请求失败,会来到"/login?error"
http.formLogin().passwordParameter("pwd").usernameParameter("user").loginPage("/userlogin");
//开启注销功能
http.logout().logoutSuccessUrl("/");
//开启记住我功能
http.rememberMe().rememberMeParameter("remember");
}
//定义认证规则
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder() ).withUser("zhangfei").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2")
.and()
.passwordEncoder(new BCryptPasswordEncoder() ).withUser("lvbu").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
.and()
.passwordEncoder(new BCryptPasswordEncoder() ).withUser("liubei").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
}
}
注1:クリアコードの暗号化
Spring Security 5.X以降では、パスワードエンコーダーを使用する必要があります。つまり、プレーンテキストのパスワードを暗号化する必要があり、パスワードエンコーダーNoAppasswordEncoderを使用することはできません。
注2:HttpSecurity
はログインおよびログアウト機能を開きます
注3:セキュリティとthymeleafの相互作用
thymlefのセキュリティサポートの使用:
依存関係を導入する必要があります:
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
セキュリティ-thymeleaf名前空間の制約:
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
isAuthenticated()を使用して、ログインしているインターフェイスとログインしていないインターフェイスを区別します。
<div sec:authorize="!isAuthenticated()">
<h2 align="center">游客您好,如果想查看武林秘籍 <a th:href="@{/userlogin}">请登录</a></h2>
</div>
<div sec:authorize="isAuthenticated()">
<h2><span sec:authentication="name"></span>,您好,您的角色有:<span sec:authentication="principal.authorities"></span></h2>
<form th:action="@{/logout}" method="post">
<input type="submit" value="注销"/>
</form>
</div>
ロールに応じて汎用モジュールを分割します。
<div sec:authorize="hasRole('vip1')">
<h3>普通武功秘籍</h3>
<ul>
<li><a th:href="@{/level1/1}">罗汉拳</a></li>
<li><a th:href="@{/level1/2}">武当长拳</a></li>
<li><a th:href="@{/level1/3}">全真剑法</a></li>
</ul>
</div>
注4:Remember me機能
ログインに成功すると、保存のためにCookieがブラウザに送信され、チェックに合格すれば、今後アクセスしたときにクッケイが持参されます。ログインできます。通常は。このクッキーは、ログアウトをクリックすると削除されます。
注5:ランディングページをカスタマイズできます
http.formLogin().passwordParameter("pwd").usernameParameter("user").loginPage("/userlogin");
//开启记住我功能
http.rememberMe().rememberMeParameter("remember");
<form th:action="@{/userlogin}" method="post">
用户名:<input name="user"/><br/>
密码:<input name="pwd"/><br/>
<input type="checkbox" name="remember"/> 记住我<br/>
<input type="submit" value="登陆"/>
</form>