SpringSecurityカスタムフォームのログイン
どのようにカスタムフォームのログインSpringSecurityは、デフォルトのログインフォームを提供していますが、確かに実際のプロジェクトでは、本明細書に使用されていない、これは主にフォーム・ログインをカスタマイズする方法を説明します、この章のショーに説明SpringSecurity
プロジェクトを作成しますSpringSecurity 1
1.1 IDEA
SpringBootは、最初にWebに依存し、IDEAと依存SpringSecurityてプロジェクトを作成します
この時点で、自動的にのpom.xmlに追加されます
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2.扩展WebSecurityConfigurerAdapter
WebSecurityConfigurerAdapter SpringSecurityは、私たち自身のコンフィギュレーションの拡大のために提供されます
WebSecurityConfigurerAdapterを達成するため、多くの場合、書き換えることする必要があります。
1、configure(AuthenticationManagerBuilder auth);
2、configure(WebSecurity web);
3、configure(HttpSecurity http);
いくつかの基本的な構成は次の通りであると2.1のデフォルトWebSecurityConfigurerAdapterたちを提供します
protected void configure(HttpSecurity http) throws Exception {
logger.debug("Using default configure(HttpSecurity). If subclassed this will potentially override subclass configure(HttpSecurity).");
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin().and()
.httpBasic();
}
2.2カスタムWebSecurityConfigurerを作成します
FormLoginConfigurer HttpSecurityに適用される1.formLogin()オープンログフォームは、フォローアップは、フィルタ対応に変換される
()を設定するカスタムフォームページ2.loginPageを
3.authorizeRequests()。AnyRequest()。認証() ;認定されなければならない任意の要求インタフェースを参照してください。
@Configuration
@Slf4j
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.formLogin()
.loginPage("/mylogin.html")
.and()
.authorizeRequests().anyRequest().authenticated();
}
}
2.3 mylogin.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>标准登录页面</h1>
<h3>表单登录</h3>
<form action="/login" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td colspan="2">
<button type="submit">登录</button>
</td>
</tr>
</table>
</form>
</body>
</html>
3.アクセスカスタムログインページ(あまりにも多くのリダイレクトの問題があることに注意してください)
プロジェクトとの直接のアクセスを開始します
http://localhost:8080
報告書は、ブラウザが、これはそれを引き起こすもので、あまりにも多くの時間を見つけるリダイレクト?
これは、我々がloginPage(「/ mylogin.html」)の上に構成されているためですが、それへのパスがアクセスを許可されていない、/mylogin.htmlパスにリダイレクトするとき、あること、または認証が必要なので、ロード/ myloginをリダイレクトされます.htmlのは、エラーの原因となりました
4.允许登录页面路径访问 antMatchers("/mylogin.html").permitAll()
只需要在配置的地方 添加 .antMatchers("/mylogin.html").permitAll() 允许这个路径
http.csrf().disable()
.formLogin()
.loginPage("/mylogin.html")
.and()
.authorizeRequests()
.antMatchers("/mylogin.html").permitAll()
.anyRequest().authenticated();
再次访问,我们自定义的表单就显示出来了(忽略样式。。。)
此时我们输入用户名 user 密码 : 控制台打印
Using generated security password: 6bf253eb-c785-42b6-b147-b0fe2971586e
发现又跳转到 /mylogin.html页面,这是因为 当我们配置了 loginPage("/mylogin.html")之后 处理表单登录的过滤器它所拦截的请求就不再是 /login (默认是 /login) ,拦截的登录请求地址变成了 和 loginPage一样的 mylogin.html
此时如果将 action地址改成 /mylogin.html ,那么再登录 就能成功
<form action="/mylogin.html" method="post">
5.配置自定义登录接口路径 loginProcessingUrl
由于我们上面配置了 loginPage ,则对应登录接口路径也变成了 loginPage所配置的 mylogin.html,但是当我们不想使用这个作为接口路径的时候,可以通过以下配置来修改
通过 loginProcessingUrl 类配置处理登录请求的路径
http.csrf().disable()
.formLogin()
.loginPage("/mylogin.html")
.loginProcessingUrl("/auth/login")
.and()
.authorizeRequests()
.antMatchers("/mylogin.html").permitAll()
.anyRequest().authenticated();
记得和action 对应
<form action="/auth/login" method="post">
至此SpringSecurity自定义登录页面的配置 以及注意事项全部说完
6. 总结
本篇主要讲解 在SpringSecurity中 如何 自定义表单登, 是不是非常简单 ,但是也有一些要注意的点
1.扩展WebSecurityConfigurerAdapter
2.配置loginPage 页面路径
3.允许loginPage 页面路径 访问
4.配置登录请求的路径 loginProcessingUrl
個人のブログのアドレス:https://www.askajohnny.comようこそ!
ブログ記事複数のプラットフォームからこの記事OpenWriteリリース!