Spring security登录实现原理是什么(security,spring,登录,编程语言)

时间:2024-05-02 12:28:09 作者 : 石家庄SEO 分类 : 编程语言
  • TAG :

1. 新建项目

引入web和security包

完整的pom.xml文件如下

<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="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.0https://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/><!--lookupparentfromrepository--></parent><groupId>com.example</groupId><artifactId>spring-demo</artifactId><version>0.0.1-SNAPSHOT</version><name>spring-demo</name><description>DemoprojectforSpringBoot</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. 编写启动类和控制器方法和自定义登录页面

packagecom.example.springdemo;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;@RestController@SpringBootApplicationpublicclassSpringDemoApplication{publicstaticvoidmain(String[]args){SpringApplication.run(SpringDemoApplication.class,args);}@GetMapping("/")publicStringhello(){return"hellospringsecurity";}}
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><body><formaction="myLogin.html"method="post"><inputtype="text"name="username"><inputtype="password"name="password"><inputtype="submit"value="登录"></form></body></html>

3. 编写配置类

packagecom.example.springdemo.conf;importorg.springframework.security.config.annotation.web.builders.HttpSecurity;importorg.springframework.security.config.annotation.web.configuration.EnableWebSecurity;importorg.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;importorg.springframework.security.core.Authentication;importorg.springframework.security.core.AuthenticationException;importorg.springframework.security.web.authentication.AuthenticationFailureHandler;importorg.springframework.security.web.authentication.AuthenticationSuccessHandler;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;importjava.io.PrintWriter;@EnableWebSecuritypublicclassWebSecurityConfigextendsWebSecurityConfigurerAdapter{@Overrideprotectedvoidconfigure(HttpSecurityhttp)throwsException{http.authorizeRequests().anyRequest().authenticated().and().formLogin()//指定处理登录页面.loginPage("/myLogin.html")//指定登录成功的处理逻辑.successHandler(newAuthenticationSuccessHandler(){@OverridepublicvoidonAuthenticationSuccess(HttpServletRequestrequest,HttpServletResponseresponse,Authenticationauthentication)throwsIOException,ServletException{response.setContentType("application/json;charset=UTF-8");PrintWriterwriter=response.getWriter();writer.write("{\"error_code\":\"0\",\"message\":\"欢迎登录\"}");}})//指定登录失败时的处理逻辑.failureHandler(newAuthenticationFailureHandler(){@OverridepublicvoidonAuthenticationFailure(HttpServletRequestrequest,HttpServletResponseresponse,AuthenticationExceptione)throwsIOException,ServletException{response.setStatus(401);PrintWriterwriter=response.getWriter();writer.write("{\"error_code\":\"401\",\"name\":\""+e.getClass()+"\",\"message\":\""+e.getMessage()+"\"}");}}).permitAll().and().csrf().disable();}}

4. 运行结果

当输入密码错误时

Spring security登录实现原理是什么

当输入密码正确时

Spring security登录实现原理是什么

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Spring security登录实现原理是什么的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Struts2怎么返回json格式的数据下一篇:

5 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18