av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Spring security登錄過程邏輯詳解

瀏覽:121日期:2023-09-12 15:08:01

1. 新建項目

引入web和security包

完整的pom.xml文件如下

<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>spring-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions><exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId></exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>

2. 編寫啟動類和控制器方法和自定義登錄頁面

package com.example.springdemo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestController@SpringBootApplicationpublic class SpringDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringDemoApplication.class, args); } @GetMapping('/') public String hello() { return 'hello spring security'; }}

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title></head><body><form action='myLogin.html' method='post'> <input type='text' name='username'> <input type='password' name='password'> <input type='submit' value='登錄'></form></body></html>

3. 編寫配置類

package com.example.springdemo.conf;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.Authentication;import org.springframework.security.core.AuthenticationException;import org.springframework.security.web.authentication.AuthenticationFailureHandler;import org.springframework.security.web.authentication.AuthenticationSuccessHandler;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.PrintWriter;@EnableWebSecuritypublic class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().anyRequest().authenticated().and().formLogin()//指定處理登錄頁面.loginPage('/myLogin.html')//指定登錄成功的處理邏輯.successHandler(new AuthenticationSuccessHandler() { @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { response.setContentType('application/json;charset=UTF-8'); PrintWriter writer = response.getWriter(); writer.write('{'error_code':'0','message':'歡迎登錄'}'); }})//指定登錄失敗時的處理邏輯.failureHandler(new AuthenticationFailureHandler() { @Override public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException { response.setStatus(401); PrintWriter writer = response.getWriter(); writer.write('{'error_code':'401','name':'' + e.getClass() + '','message':'' + e.getMessage() + ''}'); }}).permitAll().and().csrf().disable(); }}

4. 運行結果

當輸入密碼錯誤時

Spring security登錄過程邏輯詳解

當輸入密碼正確時

Spring security登錄過程邏輯詳解

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 毛片久久久 | 亚洲人成免费 | 国产一区二区在线免费 | 五月av| 亚洲天堂男人 | 黄色av免费 | 韩国精品一区 | 天天爽夜夜爽夜夜爽 | 色婷婷狠狠| 一本到av | 亚洲欧美日韩一区 | 成人三级视频在线观看 | 欧美久久久久久久久 | 嫩草在线观看 | 三级视频在线 | 国产视频在线观看视频 | 中文字幕网址在线 | 亚洲a级片 | 亚洲欧美精品 | 国产中文字幕在线 | 91久久久久久久久久 | 亚洲在线免费观看 | 国产色婷婷 | 国产成人午夜 | av狠狠干 | 欧美成年人视频 | 麻豆中文字幕 | 最新理论片 | 欧美日韩免费一区二区三区 | 午夜激情视频在线观看 | 91久久国产综合久久91精品网站 | 日韩福利片 | 成人在线免费观看视频 | 国产精品自拍第一页 | 精品视频免费 | www.粉色视频在线观看 | 在线色网站 | 拍床戏真做h文黄肉1v1 | 国产精品黄色 | 91日韩在线 | 日韩欧美精品一区二区 |