SpringSecurity注销怎么设置
导读:本文共7475字符,通常情况下阅读需要25分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要:这篇“SpringSecurity注销怎么设置”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“SpringSecurity注销怎么设置”文章吧。Spring Security中也提供了默认的注销配置,在开发时也可以按照自己需求对注销进行个性化定制开启注销 默认开启packagecom.... ...
目录
(为您整理了一些要点),点击可以直达。这篇“SpringSecurity注销怎么设置”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“SpringSecurity注销怎么设置”文章吧。
Spring Security中也提供了默认的注销配置,在开发时也可以按照自己需求对注销进行个性化定制
开启注销 默认开启
packagecom.example.config;importcom.example.handler.MyAuthenticationFailureHandler;importcom.example.handler.MyAuthenticationSuccessHandler;importorg.springframework.context.annotation.Configuration;importorg.springframework.security.config.annotation.web.builders.HttpSecurity;importorg.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@ConfigurationpublicclassWebSecurityConfigurerextendsWebSecurityConfigurerAdapter{@Overridepublicvoidconfigure(HttpSecurityhttp)throwsException{//【注意事项】放行资源要放在前面,认证的放在后面http.authorizeRequests().mvcMatchers("/index").permitAll()//代表放行index的所有请求.mvcMatchers("/loginHtml").permitAll()//放行loginHtml请求.anyRequest().authenticated()//代表其他请求需要认证.and().formLogin()//表示其他需要认证的请求通过表单认证//loginPage一旦你自定义了这个登录页面,那你必须要明确告诉SpringSecurity日后哪个url处理你的登录请求.loginPage("/loginHtml")//用来指定自定义登录界面,不使用SpringSecurity默认登录界面注意:一旦自定义登录页面,必须指定登录url//loginProcessingUrl这个doLogin请求本身是没有的,因为我们只需要明确告诉SpringSecurity,日后只要前端发起的是一个doLogin这样的请求,//那SpringSecurity应该把你username和password给捕获到.loginProcessingUrl("/doLogin")//指定处理登录的请求url.usernameParameter("uname")//指定登录界面用户名文本框的name值,如果没有指定,默认属性名必须为username.passwordParameter("passwd")//指定登录界面密码密码框的name值,如果没有指定,默认属性名必须为password//.successForwardUrl("/index")//认证成功forward跳转路径,forward代表服务器内部的跳转之后,地址栏不变始终在认证成功之后跳转到指定请求//.defaultSuccessUrl("/index")//认证成功之后跳转,重定向redirect跳转后,地址会发生改变根据上一保存请求进行成功跳转.successHandler(newMyAuthenticationSuccessHandler())//认证成功时处理前后端分离解决方案//.failureForwardUrl("/loginHtml")//认证失败之后forward跳转//.failureUrl("/login.html")//认证失败之后redirect跳转.failureHandler(newMyAuthenticationFailureHandler())//用来自定义认证失败之后处理前后端分离解决方案.and().logout().logoutUrl("/logout")//指定注销登录url默认请求方式必须:GET.invalidateHttpSession(true)//会话失效默认值为true,可不写.clearAuthentication(true)//清除认证标记默认值为true,可不写.logoutSuccessUrl("/loginHtml")//注销成功之后跳转页面.and().csrf().disable();//禁止csrf跨站请求保护}}
通过logout()方法开启注销配置
logoutUrl指定退出登录请求地址,默认是GET请求,路径为/logout
invalidateHttpSession 退出时是否是session失效,默认值为true
clearAuthentication 退出时是否清除认证信息,默认值为true
logoutSuccessUrl 退出登录时跳转地址
测试
先访问http://localhost:8080/hello,会自动跳转到http://localhost:8080/loginHtml登录界面,输入用户名和密码,地址栏会变化为:http://localhost:8080/doLogin,然后重新访问http://localhost:8080/hello,此时可以看到hello的数据,表示登录认证
成功然后访问http://localhost:8080/logout,会自动跳转到http://localhost:8080/loginHtml登录页面,然后在访问http://localhost:8080/hello,发现会跳转到http://localhost:8080/loginHtml登录界面,表示后端认定你未登录了,需要你登录认证
配置多个注销登录请求
如果项目中也有需要,开发者还可以配置多个注销登录的请求,同时还可以指定请求的方法
新增退出controller
packagecom.example.controller;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;@ControllerpublicclassLogoutController{@RequestMapping("/logoutHtml")publicStringlogout(){return"logout";}}
新增退出界面
logout.html
<!DOCTYPEhtml><htmlxmlns:th="http://www.thymeleaf.org/"lang="en"><head><metacharset="UTF-8"><title>注销</title></head><body><h2>注销</h2><formth:action="@{/bb}"method="post"><inputtype="submit"value="注销"></form></body></html>
修改配置信息
packagecom.example.config;importcom.example.handler.MyAuthenticationFailureHandler;importcom.example.handler.MyAuthenticationSuccessHandler;importorg.springframework.context.annotation.Configuration;importorg.springframework.security.config.annotation.web.builders.HttpSecurity;importorg.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;importorg.springframework.security.web.util.matcher.AntPathRequestMatcher;importorg.springframework.security.web.util.matcher.OrRequestMatcher;@ConfigurationpublicclassWebSecurityConfigurerextendsWebSecurityConfigurerAdapter{@Overridepublicvoidconfigure(HttpSecurityhttp)throwsException{//【注意事项】放行资源要放在前面,认证的放在后面http.authorizeRequests().mvcMatchers("/index").permitAll()//代表放行index的所有请求.mvcMatchers("/loginHtml").permitAll()//放行loginHtml请求.anyRequest().authenticated()//代表其他请求需要认证.and().formLogin()//表示其他需要认证的请求通过表单认证//loginPage一旦你自定义了这个登录页面,那你必须要明确告诉SpringSecurity日后哪个url处理你的登录请求.loginPage("/loginHtml")//用来指定自定义登录界面,不使用SpringSecurity默认登录界面注意:一旦自定义登录页面,必须指定登录url//loginProcessingUrl这个doLogin请求本身是没有的,因为我们只需要明确告诉SpringSecurity,日后只要前端发起的是一个doLogin这样的请求,//那SpringSecurity应该把你username和password给捕获到.loginProcessingUrl("/doLogin")//指定处理登录的请求url.usernameParameter("uname")//指定登录界面用户名文本框的name值,如果没有指定,默认属性名必须为username.passwordParameter("passwd")//指定登录界面密码密码框的name值,如果没有指定,默认属性名必须为password//.successForwardUrl("/index")//认证成功forward跳转路径,forward代表服务器内部的跳转之后,地址栏不变始终在认证成功之后跳转到指定请求//.defaultSuccessUrl("/index")//认证成功之后跳转,重定向redirect跳转后,地址会发生改变根据上一保存请求进行成功跳转.successHandler(newMyAuthenticationSuccessHandler())//认证成功时处理前后端分离解决方案//.failureForwardUrl("/loginHtml")//认证失败之后forward跳转//.failureUrl("/login.html")//认证失败之后redirect跳转.failureHandler(newMyAuthenticationFailureHandler())//用来自定义认证失败之后处理前后端分离解决方案.and().logout()//.logoutUrl("/logout")//指定注销登录url默认请求方式必须:GET.logoutRequestMatcher(newOrRequestMatcher(newAntPathRequestMatcher("/aa","GET"),newAntPathRequestMatcher("/bb","POST"))).invalidateHttpSession(true)//会话失效默认值为true,可不写.clearAuthentication(true)//清除认证标记默认值为true,可不写.logoutSuccessUrl("/loginHtml")//注销成功之后跳转页面.and().csrf().disable();//禁止csrf跨站请求保护}}
测试
GET /aa 跟上面测试方法一样
POST /bb
先访问http://localhost:8080/hello,会自动跳转到http://localhost:8080/loginHtml登录界面,输入用户名和密码,地址栏会变化为:http://localhost:8080/doLogin,然后重新访问http://localhost:8080/hello,此时可以看到hello的数据,表示登录认证成功
然后访问http://localhost:8080/logoutHtml,会跳出注销页面,点击“注销”按钮,会返回到http://localhost:8080/loginHtml登录界面,再重新访问http://localhost:8080/hello,发现会跳转到http://localhost:8080/loginHtml登录界面,表示注销成功,需要重新登录。
前后端分离注销配置
如果是前后端分离开发,注销成功之后就不需要页面跳转了,只需要将注销成功的信息返回前端即可,此时我们可以通过自定义LogoutSuccessHandler实现来返回内容注销之后信息
添加handler
packagecom.example.handler;importcom.fasterxml.jackson.databind.ObjectMapper;importorg.springframework.security.core.Authentication;importorg.springframework.security.web.authentication.logout.LogoutSuccessHandler;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;importjava.util.HashMap;importjava.util.Map;/***自定义注销成功之后处理*/publicclassMyLogoutSuccessHandlerimplementsLogoutSuccessHandler{@OverridepublicvoidonLogoutSuccess(HttpServletRequestrequest,HttpServletResponseresponse,Authenticationauthentication)throwsIOException,ServletException{Map<String,Object>result=newHashMap<>();result.put("msg","注销成功,当前认证对象为:"+authentication);result.put("status",200);result.put("authentication",authentication);response.setContentType("application/json;charset=UTF-8");Strings=newObjectMapper().writeValueAsString(result);response.getWriter().println(s);}}
修改配置信息
logoutSuccessHandler
packagecom.example.config;importcom.example.handler.MyAuthenticationFailureHandler;importcom.example.handler.MyAuthenticationSuccessHandler;importcom.example.handler.MyLogoutSuccessHandler;importorg.springframework.context.annotation.Configuration;importorg.springframework.security.config.annotation.web.builders.HttpSecurity;importorg.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;importorg.springframework.security.web.util.matcher.AntPathRequestMatcher;importorg.springframework.security.web.util.matcher.OrRequestMatcher;@ConfigurationpublicclassWebSecurityConfigurerextendsWebSecurityConfigurerAdapter{@Overridepublicvoidconfigure(HttpSecurityhttp)throwsException{//【注意事项】放行资源要放在前面,认证的放在后面http.authorizeRequests().mvcMatchers("/index").permitAll()//代表放行index的所有请求.mvcMatchers("/loginHtml").permitAll()//放行loginHtml请求.anyRequest().authenticated()//代表其他请求需要认证.and().formLogin()//表示其他需要认证的请求通过表单认证//loginPage一旦你自定义了这个登录页面,那你必须要明确告诉SpringSecurity日后哪个url处理你的登录请求.loginPage("/loginHtml")//用来指定自定义登录界面,不使用SpringSecurity默认登录界面注意:一旦自定义登录页面,必须指定登录url//loginProcessingUrl这个doLogin请求本身是没有的,因为我们只需要明确告诉SpringSecurity,日后只要前端发起的是一个doLogin这样的请求,//那SpringSecurity应该把你username和password给捕获到.loginProcessingUrl("/doLogin")//指定处理登录的请求url.usernameParameter("uname")//指定登录界面用户名文本框的name值,如果没有指定,默认属性名必须为username.passwordParameter("passwd")//指定登录界面密码密码框的name值,如果没有指定,默认属性名必须为password//.successForwardUrl("/index")//认证成功forward跳转路径,forward代表服务器内部的跳转之后,地址栏不变始终在认证成功之后跳转到指定请求//.defaultSuccessUrl("/index")//认证成功之后跳转,重定向redirect跳转后,地址会发生改变根据上一保存请求进行成功跳转.successHandler(newMyAuthenticationSuccessHandler())//认证成功时处理前后端分离解决方案//.failureForwardUrl("/loginHtml")//认证失败之后forward跳转//.failureUrl("/login.html")//认证失败之后redirect跳转.failureHandler(newMyAuthenticationFailureHandler())//用来自定义认证失败之后处理前后端分离解决方案.and().logout()//.logoutUrl("/logout")//指定注销登录url默认请求方式必须:GET.logoutRequestMatcher(newOrRequestMatcher(newAntPathRequestMatcher("/aa","GET"),newAntPathRequestMatcher("/bb","POST"))).invalidateHttpSession(true)//会话失效默认值为true,可不写.clearAuthentication(true)//清除认证标记默认值为true,可不写//.logoutSuccessUrl("/loginHtml")//注销成功之后跳转页面.logoutSuccessHandler(newMyLogoutSuccessHandler())//注销成功之后处理前后端分离解决方案.and().csrf().disable();//禁止csrf跨站请求保护}}
测试
跟第一个测试方法是一样的
以上就是关于“SpringSecurity注销怎么设置”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。
SpringSecurity注销怎么设置的详细内容,希望对您有所帮助,信息来源于网络。