当前位置: 首页 > news >正文

做网站前端需要懂得网上服务办事大厅

做网站前端需要懂得,网上服务办事大厅,重庆建设工程信息网官网中苏业盛,wordpress 模版 怎么用Lison dreamlison163.com, v1.0.0, 2024.06.01 Spring Security-02-Spring Security认证方式-HTTP基本认证、Form表单认证、HTTP摘要认证、前后端分离安全处理方案 文章目录 Spring Security-02-Spring Security认证方式-HTTP基本认证、Form表单认证、HTTP摘要认证、…Lison dreamlison163.com, v1.0.0, 2024.06.01 Spring Security-02-Spring Security认证方式-HTTP基本认证、Form表单认证、HTTP摘要认证、前后端分离安全处理方案 文章目录 Spring Security-02-Spring Security认证方式-HTTP基本认证、Form表单认证、HTTP摘要认证、前后端分离安全处理方案认证方式HTTP基本认证基本认证简介基本认证核心API基本认证的步骤基本认证的弊端HTTP基本认证代码实现创建SecurityConfig配置类测试验证 Basic认证详解基本认证过程注销Basic认证 Form表单认证表单认证简介表单认证效果表单认证中的预置url和页面自定义表单认证配置创建SecurityConfig配置类测试应用 自定义表单认证的登录界面服务端代码定义页面定义测试应用 细化表单认证配置定义SecurityConfig类自定义登录页面定义错误处理页面测试验证 HTTP摘要认证HTTP摘要认证简介HTTP摘要认证核心参数摘要认证代码实现编写测试接口创建SecurityConfig配置类测试接口HTTP摘要认证弊端 前后端分离时的安全处理方案前后端分离简介认证处理时的相关API页面跳转的相关API返回JSON格式的处理器 认证成功时的处理方案1. successHandler()方法2. onAuthenticationSuccess参数定义SecurityAuthenticationSuccessHandler类配置successHandler验证结果 认证失败时的处理方案failureHandler()代码实现配置failureHandler验证结果 退出登录时的处理方案1. logoutSuccessHandler()2. 定义SecurityLogoutSuccessHandler类配置logoutSuccessHandler验证结果 未认证时的处理方案1. authenticationEntryPoint()2. 定义SecurityAuthenticationEntryPoint类配置authenticationEntryPoint验证结果 认证方式 认证: 所谓的认证就是用来判断系统中是否存在某用户并判断该用户的身份是否合法的过程解决的其实是用户登录的问题。认证的存在是为了保护系统中的隐私数据与资源只有合法的用户才可以访问系统中的资源。 在Spring Security中常见的认证方式可以分为HTTP层面和表单层面常见的认证方式如下: HTTP基本认证Form表单认证;HTTP摘要认证 HTTP基本认证 基本认证简介 在Spring Security 4.x版本中默认采用的登录方式是Http基本认证该方式会弹出一个对话框要求用户输入用户名和密码。在每次进行基本认证请求时都会在Authorization请求头中利用Base64对 “用户:密码” 字符串进行编码。这种方式并不安全并不适合在Web项目中使用但它是一些现代主流认证的基础而且在Spring Security的OAuth中内部认证的默认方式就是用的Http基本认证。 基本认证核心API 执行流程如下: Filter-构造Token-AuthenticationManager-转给Provider处理-认证处理成功后续操作或者不通过抛异常基本认证的步骤 HTTP基本认证是在RFC2616标准中定义的一种认证模式它以一种很简单的方式与用户进行交互。HTTP基本认证可以分为如下4个步骤 ①. 客户端首先发起一个未携带认证信息的请求②. 然后服务器端返回一个401 Unauthorized的响应信息并在WWW-Authentication头部中说明认证形式当进行HTTP基本认证时WWW-Authentication会被设置为Basic realm“被保护的页面”③. 接下来客户端会收到这个401 Unauthorized响应信息并弹出一个对话框询问用户名和密码。当用户输入后客户端会将用户名和密码使用冒号进行拼接并用Base64编码然后将其放入到请求的Authorization头部并发送给服务器④. 最后服务器端对客户端发来的信息进行解码得到用户名和密码并对该信息进行校验判断是否正确最终给客户端返回响应内容。 基本认证的弊端 HTTP基本认证是一种无状态的认证方式与表单认证相比HTTP基本认证是一种基于HTTP层面的认证方式无法携带Session信息也就无法实现Remember-Me功能。另外用户名和密码在传递时仅做了一次简单的Base64编码几乎等同于以明文传输极易被进行密码窃听和重放攻击。所以在实际开发中很少会使用这种认证方式来进行安全校验。 HTTP基本认证代码实现 创建SecurityConfig配置类 这里我们先创建一个config配置类命名为SecurityConfig并且继承自WebSecurityConfigurerAdapter父类代码如下: package com.lison.springsecurity.config.security;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;/*** className: com.lison.springsecurity.config.security- SecurityConfig* description:* author: Lison* createDate: 2024-06-01*/ Configuration EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {Overrideprotected void configure(HttpSecurity http) throws Exception {//1.配置基本认证方式http.authorizeRequests()//对任意请求都进行认证.anyRequest().authenticated().and()//开启basic认证.httpBasic();} }httpBasic()方法就是用来开启基本认证的而且默认采用的就是基本认证 测试验证 访问自己的/hello接口这时候我们就可以看到浏览器中弹出了一个登陆窗口。提示我们输入自己的用户名和密码 lison/123456认证成功后即可访问自己的web接口。 Basic认证详解 基本认证过程 此时的响应码为401什么情况下会导致401状态码? 根据401和以上响应头信息浏览器会弹出一个对话框要求输入 用户名/密码Basic认证会将其拼接成 “用户名:密码” 格式中间是一个冒号并利用Base64编码成加密字符串xxx然后在请求头中附加 Authorization: Basic xxx 信息发送给后台认证后台需要利用Base64来进行解码xxx得到用户名和密码再校验 用户名:密码 信息。 如果认证错误浏览器会保持弹框如果认证成功浏览器会缓存有效的Base64编码在之后的请求中浏览器都会在请求头中添加该有效编码。 注销Basic认证 在成功认证之后Basic认证会把Authorization认证信息缓存在浏览器中一段时间之后每次请求接口时都会自动带上所以直到 用户关闭浏览器才会销毁认证信息也就是说我们无法在服务端进行有效的注销。 不过在请求注销时前端也可以手动 在请求头配置一个错误的Authorization或者在浏览器的命令行执行 document.execuCommand(“ClearAuthenticationCache”)方法 来清空认证信息但该方式对Chrome浏览器无效。我们在调试基本认证时可以直接开启无痕模式避免很多因为缓存造成的问题。 Form表单认证 表单认证简介 对于表单认证其实在SpringBoot开发环境中只要我们添加了Spring Security的依赖包就会自动实现表单认证。在WebSecurityConfigurerAdapter类的config(HttpSecurity http)方法中可以看到如下默认实现。 在SpringBoot环境中默认支持的就是表单认证方式。 表单认证效果 第一个Spring Security项目中实现的效果其实就是表单认证。每次我们在访问某个Web接口之前都会重定向到一个Security自带的login登录页面上这个登录页面就是表单认证的效果 表单认证中的预置url和页面 这时候有的小伙伴可能就会很好奇为什么表单认证会有以上效果这是因为在默认的formLogin配置中自动配置了一些url和页面: /login(get): get请求时会跳转到这个页面只要我们访问任意一个需要认证的请求时都会跳转到这个登录界面。/login(post): post请求时会触发这个接口在登录页面点击登录时默认的登录页面表单中的action就是关联这个login接口。/login?error: 当用户名或密码错误时会跳转到该页面。/: 登录成功后默认跳转到该页面如果配置了index.html页面则 ”/“ 会重定向到index.html页面当然这个页面要由我们自己实现。/logout: 注销页面。/login?logout: 注销成功后跳转到的页面。 由此可见SpringSecurity默认有两个login即登录页面和登录接口的地址都是 /login: GET http://localhost:8080/loginPOST http://localhost:8080/login 如果是GET 请求表示你想访问登录页面如果是 POST 请求表示你想提交登录数据。 对于这几个URL接口我们简单了解即可。 自定义表单认证配置 创建SecurityConfig配置类 我们先编写一个类继承自WebSecurityConfigurerAdapter父类该类的作用如下: 验证所有请求允许用户使用表达登录进行身份验证允许用户使用Http基本认证 package com.lison.springsecurity.config.security;import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 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;/*** className: com.lison.springsecurity.config.security- SecurityConfig* description:* author: Lison* createDate: 2024-06-01*/Configuration EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {Overrideprotected void configure(HttpSecurity http) throws Exception {//配置表单认证方式http.authorizeRequests().anyRequest().authenticated().and()//开启表单认证.formLogin();} } SecurityConfig类上添加EnableWebSecurity注解后会自动被Spring发现并注册。在configure()方法中我执行了formLogin()方法该方法的功能就是开启表单认证。 测试应用 启动项目访问我们定义的/hello接口时首先会重定向到/login页面。输入自己配置的用户名和密码后才可以正常访问/hello接口。 当认证成功后内部再次发生了302重定向:可见从/login接口重定向到了/hello接口。 自定义表单认证的登录界面 Spring Security一个特点就在于可以高度自定义灵活配置可以自定义一个登录页面。 服务端代码定义 package com.lison.springsecurity.config.security;import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;/*** className: com.lison.springsecurity.config.security- SecurityConfig* description:* author: Lison* createDate: 2024-06-01*/Configuration EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {/*** 用来定义哪些请求需要忽略安全控制哪些请求必须接受安全控制;还可以在合适的时候清除SecurityContext以避免内存泄漏* 同时也可以用来定义请求防火墙和请求拒绝处理器另外我们开启Spring Security Debug模式也是这里配置的*/Overridepublic void configure(WebSecurity web) throws Exception {//super.configure(web);web.ignoring().antMatchers(/js/**, /css/**, /images/**);}Overrideprotected void configure(HttpSecurity http) throws Exception {//2.配置自定义的登录页面http.authorizeRequests().anyRequest().authenticated().and().formLogin()//加载自定义的登录页面地址.loginPage(/myLogin.html).permitAll().and()//注意:需禁用crsf防护功能,否则登录不成功.csrf().disable();} } 1、WebSecurity执行流程 在configure(WebSecurity web)方法中有个核心参数WebSecurity类在这个类里定义了一个securityFilterChainBuilders集合可以同时管理多个SecurityFilterChain过滤器链各位可以回顾我们学习Web基础时关于过滤器的知识点这些过滤器的执行是不是比Servlet更早 当WebSecurity在执行时会构建出一个名为 ”springSecurityFilterChain“ 的 Spring BeanFilterChainProxy代理类它的作用是来 定义哪些请求可以忽略安全控制哪些请求必须接受安全控制以及在合适的时候 清除SecurityContext 以避免内存泄漏同时也可以用来 定义请求防火墙和请求拒绝处理器也可以在这里 开启Spring Security 的Debug模式。 上面这一系列的Filter过滤器我们就可以利用web.ignoring() 方法来配置想要忽略的静态资源 URL 地址这样这些静态资源就可以不被拦截从而可以被识别访问 2、HttpSecurity作用 HttpSecurity用来构建包含一系列的过滤器链SecurityFilterChain平常我们的配置就是围绕着这个SecurityFilterChain进行。 页面定义 首先自定义一个登陆页面主要是编写html代码和css样式其核心代码如下: !DOCTYPE html html langen headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleLogin Page/titlestylebody {background-color: #f2f2f2;font-family: Arial, sans-serif;}.container {max-width: 400px;margin: 0 auto;padding: 40px;background-color: #fff;border-radius: 5px;box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);}h2 {text-align: center;margin-bottom: 30px;}.form-group {margin-bottom: 20px;}label {display: block;font-weight: bold;margin-bottom: 5px;}input[typetext],input[typepassword] {width: 100%;padding: 10px;border: 1px solid #ccc;border-radius: 3px;}.btn {display: block;width: 100%;padding: 10px;background-color: #4caf50;color: #fff;font-weight: bold;text-align: center;text-decoration: none;border: none;border-radius: 3px;cursor: pointer;}.btn:hover {background-color: #45a049;}/style /head body div classcontainerh2Login/h2form action/myLogin.html methodpostdiv classform-grouplabel forusernameUsername:/labelinput typetext idusername nameusername placeholderEnter your username/divdiv classform-grouplabel forpasswordPassword:/labelinput typepassword idpassword namepassword placeholderEnter your password/divbutton classbtn typesubmitLogin/button/form /div /body /html测试应用 启动项目后我们访问接口时就会自动跳转到自己定义的/myLogin.html页面上输入用户名和密码后就可以成功访问自己的接口。 细化表单认证配置 修改表单认证页面中请求参数的名称定义认证失败时的错误处理页面处理退出登录时的操作等这些都可以自定义配置实现代码如下。 定义SecurityConfig类 package com.lison.springsecurity.config.security;import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;/*** className: com.lison.springsecurity.config.security- SecurityConfig* description:* author: Lison* createDate: 2024-06-01*/Configuration EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {/*** 用来定义哪些请求需要忽略安全控制哪些请求必须接受安全控制;还可以在合适的时候清除SecurityContext以避免内存泄漏* 同时也可以用来定义请求防火墙和请求拒绝处理器另外我们开启Spring Security Debug模式也是这里配置的*/Overridepublic void configure(WebSecurity web) throws Exception {//super.configure(web);web.ignoring().antMatchers(/js/**, /css/**, /images/**);}Overrideprotected void configure(HttpSecurity http) throws Exception {//super.configure(http);//3.进一步配置自定义的登录页面 //拦截请求创建FilterSecurityInterceptor http.authorizeRequests().anyRequest().authenticated()//用and来表示配置过滤器结束以便进行下一个过滤器的创建和配置.and()//设置表单登录创建UsernamePasswordAuthenticationFilter.formLogin().loginPage(/myLogin.html).permitAll()//指登录成功后是否始终跳转到登录成功url。它默认为false.defaultSuccessUrl(/index.html,true)//post登录接口登录验证由系统实现.loginProcessingUrl(/login)//用户密码错误跳转接口.failureUrl(/error.html)//要认证的用户参数名默认username.usernameParameter(username)//要认证的密码参数名默认password.passwordParameter(password).and()//配置注销.logout()//注销接口.logoutUrl(/logout)//注销成功后跳转到的接口.logoutSuccessUrl(/myLogin.html).permitAll()//删除自定义的cookie.deleteCookies(myCookie).and()//注意:需禁用crsf防护功能,否则登录不成功.csrf().disable();} } 自定义登录页面 登录页面也跟着修改一下主要是把form表单中action的值修改掉为 login body div classcontainerh2Login/h2form action/login methodpostdiv classform-grouplabel forusernameUsername:/labelinput typetext idusername nameusername placeholderEnter your username/divdiv classform-grouplabel forpasswordPassword:/labelinput typepassword idpassword namepassword placeholderEnter your password/divbutton classbtn typesubmitLogin/button/form /div /body注意此时form表单中action的值要写成”/login“因为我们在配置类中通过“loginProcessingUrl(“/login”)”方法中做了明确的配置 定义错误处理页面 输入了错误的用户名和密码后可以提供一个错误处理页面当认证失败后跳转到这个页面即可 !DOCTYPE html html langen headmeta charsetUTF-8titleTitle/title /head body h1 ERROR 错误页面/h1 /body /html测试验证 如果访问自己的接口比如/hello会先重定向到/myLogin.html页面输入自己配置的用户名和密码经过验证后才会重定向到/index.html页面中否则会重定向到我们配置的/error.html页面中。 1、重定向到/myLogin.html 访问资源时会先重定向到自定义的登录页面。 2、重定向到/index.html 认证成功后会重定向到index.html页面。 3、重定向到/error.html 认证失败后会重定向到自定义的错误处理页面。 HTTP摘要认证 HTTP摘要认证简介 HTTP摘要认证和HTTP基本认证一样也是在RFC2616中定义的一种认证方式它的出现是为了弥补HTTP基本认证存在的安全隐患但该认证方式也并不是很安全**。HTTP摘要认证会使用对通信双方来说都可知的口令进行校验且最终以密文的形式来传输数据所以相对于基本认证来说稍微安全了一些。 HTTP摘要认证与基本认证类似基于简单的“挑战-回应”模型。当发起一个未经认证的请求时服务器会返回一个401回应并给客户端返回与验证相关的参数期待客户端依据这些参数继续做出回应从而完成整个验证过程 HTTP摘要认证核心参数 服务端给客户端返回的验证相关参数如下 username: 用户名。password: 用户密码。realm: 认证域由服务器返回。opaque: 透传字符串客户端应原样返回。method: 请求的方法。nonce: 由服务器生成的随机字符串包含过期时间(默认过期时间300s)和密钥。nc: 即nonce-count,指请求的次数用于计数防止重放攻击。qop被指定时nc也必须被指定。cnonce: 客户端发给服务器的随机字符串qop被指定时cnonce也必须被指定。qop: 保护级别客户端根据此参数指定摘要算法。若取值为 auth,则只进行身份验证若取值为auth-int则还需要校验内容完整性默认的qop为auth。uri: 请求的uri。response: 客户端根据算法算出的摘要值这个算法取决于qop。algorithm: 摘要算法目前仅支持MD5。entity-body: 页面实体非消息实体仅在auth-int中支持。 通常服务器端返回的数据包括realm、opaque、nonce、qop等字段如果客户端需要做出验证回应就必须按照一定的算法得到一些新的数据并一起返回**。在以上各种参数中对服务器而言最重要的字段是nonce对客户端而言最重要的字段是response**。 摘要认证代码实现 编写测试接口 package com.lison.springsecurity.controller;import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;/*** className: com.lison.springsecurity.controller- IndexController* description:* author: Lison* createDate: 2024-06-01*/ RestController public class IndexController {GetMapping(/admin/hello)public String helloAdmin() {return hello, admin;}GetMapping(/user/hello)public String helloUser() {return hello, user;}GetMapping(/visitor/hello)public String helloVisitor() {return hello, visitor;}}创建SecurityConfig配置类 这里比较重要的是配置摘要认证入口端点DigestAuthenticationEntryPoint package com.lison.springsecurity.config.security;import com.lison.springsecurity.service.MyUserDetailsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; 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.web.authentication.www.DigestAuthenticationEntryPoint; import org.springframework.security.web.authentication.www.DigestAuthenticationFilter;/*** className: com.lison.springsecurity.config.security- SecurityConfig* description:* author: Lison* createDate: 2024-06-01*/ EnableWebSecurity(debug true) public class SecurityConfig extends WebSecurityConfigurerAdapter {Autowiredprivate DigestAuthenticationEntryPoint digestAuthenticationEntryPoint;Autowiredprivate MyUserDetailsService userDetailsService;//配置认证入口端点主要是设置认证参数信息Beanpublic DigestAuthenticationEntryPoint digestAuthenticationEntryPoint(){DigestAuthenticationEntryPoint pointnew DigestAuthenticationEntryPoint();point.setKey(Security Demos);point.setRealmName(lison);point.setNonceValiditySeconds(500);return point;}public DigestAuthenticationFilter digestAuthenticationFilter(){DigestAuthenticationFilter filternew DigestAuthenticationFilter();filter.setAuthenticationEntryPoint(digestAuthenticationEntryPoint);filter.setUserDetailsService(userDetailsService);return filter;}Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers(/admin/**).hasRole(ADMIN).antMatchers(/user/**).hasRole(USER).antMatchers(/visitor/**).permitAll().anyRequest().authenticated().and().csrf().disable()//当未认证时访问某些资源,则由该认证入口类来处理..exceptionHandling().authenticationEntryPoint(digestAuthenticationEntryPoint).and()//添加自定义过滤器到过滤器链中.addFilter(digestAuthenticationFilter());}} 测试接口 访问一下需要认证的接口/admin/hello接口这时候会发现浏览器弹出了一个用户名密码的认证窗口。然后我们在浏览器中可以看到HTTP摘要认证信息realm是我们自定义的“lison”qop是默认的“auth”方式。 由此可见此时摘要认证的方式已经生效。 HTTP摘要认证弊端 HTTP摘要认证与HTTP基本认证一样都是基于HTTP层面的认证方式也不能使用Session对象因而也不支持Remember-Me功能。该方式虽然解决了HTTP基本认证中以明文传输密码的问题但并未解决密码明文存储的问题所以依然有安全隐患所以在开发中摘要认证的方式也不怎么使用。 以上三种认证方式我们需要重点掌握表单认证 前后端分离时的安全处理方案 前后端分离简介 企业开发中前后端分离已成为互联网项目开发的业界标准方式其核心思想就是前端HTML页面通过AJAX调用后端的RESTFUL API接口并使用JSON数据进行交互****。 该方式可以有效的在前后端项目之间进行解耦并且前后端分离会为以后的大型分布式架构、弹性计算架构、微服务架构、多端化服务(多种客户端例如 浏览器车载终端安卓IOS等)打下坚实的基础。 认证处理时的相关API 页面跳转的相关API 1、登录成功时的跳转API defaultSuccessUrlsuccessForwardUrl 2、登录失败时的跳转API failureUrl()failureForwardUrl() 返回JSON格式的处理器 在前后端分离模式下既然后端没有页面页面都在前端那就可以考虑使用JSON来进行信息交互了我们把认证成功或认证失败的信息以JSON的格式传递给前端由前端来决定到底该往哪个页面跳转 我们要返回JSON格式的信息有如下相关方法: successHandler()failureHandler()logoutSuccessHandler()authenticationEntryPoint()… 认证成功时的处理方案 相关的方法及其核心参数即successHandler()和onAuthenticationSuccess参数。 1. successHandler()方法 successHandler()方法的功能十分强大甚至也囊括了 defaultSuccessUrl()和 successForwardUrl() 的功能。 successHandler()方法的参数是一个 AuthenticationSuccessHandler 对象这个对象中我们要实现的方法是 onAuthenticationSuccess()。 2. onAuthenticationSuccess参数 onAuthenticationSuccess() 方法中有三个参数分别是: HttpServletRequest: 利用该参数我们可以实现服务端的跳转;HttpServletResponse: 利用该参数我们可以做客户端的跳转也可以返回 JSON 数据;Authentication: 这个参数则保存了我们刚刚登录成功的用户信息。 定义SecurityAuthenticationSuccessHandler类 r认证成功后需要处理的类要实现AuthenticationSuccessHandler 接口 /*** 处理登录成功时的业务逻辑*/ public class SecurityAuthenticationSuccessHandler implements AuthenticationSuccessHandler {/*** Authentication:携带登录的用户名及角色等信息*/Overridepublic void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {//直接输出json格式的响应信息Object principal authentication.getPrincipal();response.setContentType(application/json;charsetutf-8);PrintWriter out response.getWriter();//以json格式对外输出身份信息out.write(new ObjectMapper().writeValueAsString(principal));out.flush();out.close();} }配置successHandler 在SecurityConfig配置类中调用successHandler()方法把前面定义的SecurityAuthenticationSuccessHandler类关联进来 EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().anyRequest().authenticated().and().formLogin().permitAll()//认证成功时的处理器.successHandler(new SecurityAuthenticationSuccessHandler()).and().csrf().disable();}}验证结果 我们进行登录验证在认证成功后就可以看到登录成功的用户信息是通过 JSON 返回到前端的如下图所示: Spring Security会把认证的用户信息以JSON格式展示出来比如我们的用户名、密码、角色等信息。 认证失败时的处理方案 相关的API方法及参数。 failureHandler() failureHandler()方法的参数是一个 AuthenticationFailureHandler 对象这个对象中我们要实现的方法是 onAuthenticationFailure()。 onAuthenticationFailure()方法有三个参数分别是: HttpServletRequest: 利用该参数我们可以实现服务端的跳转;HttpServletResponse: 利用该参数我们可以做客户端的跳转也可以返回 JSON 数据;AuthenticationException: 这个参数则保存了登录失败的原因。 代码实现 同样的我们也要编写一个类SecurityAuthenticationFailureHandler实现AuthenticationFailureHandler接口来专门处理认证失败时的返回结果。 /*** 处理登录失败时的业务逻辑*/ public class SecurityAuthenticationFailureHandler implements AuthenticationFailureHandler {/*** AuthenticationException:异常信息*/Overridepublic void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {//直接输出json格式的响应信息response.setContentType(application/json;charsetutf-8);PrintWriter out response.getWriter();out.write(e.getMessage());out.flush();out.close();} }配置failureHandler 接着我们在SecurityConfig配置类中调用failureHandler()方法来关联上面定义的SecurityAuthenticationFailureHandler类对象。 核心代码如下: EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().anyRequest().authenticated().and().formLogin().permitAll()//认证成功时的处理器.successHandler(new SecurityAuthenticationSuccessHandler())//认证失败时的处理器.failureHandler(new SecurityAuthenticationFailureHandler()).and().csrf().disable();}}验证结果 配置完成后我们再去登录在认证失败时就可以看到登录失败的用户信息通过 JSON 返回到前端了 退出登录时的处理方案 认证成功和认证失败后的处理方案后,退出登录后处理方案 1. logoutSuccessHandler() 负责退出登录的方法是logoutSuccessHandler()这个方法中需要一个参数LogoutSuccessHandler在LogoutSuccessHandler类中有一个方法 onLogoutSuccess()该方法中的参数与登录成功时的参数一样。 2. 定义SecurityLogoutSuccessHandler类 我们先来定义一个SecurityLogoutSuccessHandler类实现LogoutSuccessHandler接口在这里负责输出退出登录时的JSON结果。 public class SecurityLogoutSuccessHandler implements LogoutSuccessHandler {Overridepublic void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {response.setContentType(application/json;charsetutf-8);PrintWriter out response.getWriter();out.write(注销成功);out.flush();out.close();}}配置logoutSuccessHandler 然后我们在SecurityConfig配置类中调用logoutSuccessHandler()方法来关联上面定义的SecurityLogoutSuccessHandler对象。 EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().anyRequest().authenticated().and().formLogin().permitAll()//认证成功时的处理器.successHandler(new SecurityAuthenticationSuccessHandler())//认证失败时的处理器.failureHandler(new SecurityAuthenticationFailureHandler()).and().logout()//退出登录时的处理器.logoutSuccessHandler(new SecurityLogoutSuccessHandler()).and().csrf().disable();}}验证结果 配置完成后我们去访问/logout接口退出登录成功会有如下所示结果: 未认证时的处理方案 1. authenticationEntryPoint() 未认证时同样有个专门的方法来处理即authenticationEntryPoint()方法这个方法中需要一个参数LoginUrlAuthenticationEntryPoint在LoginUrlAuthenticationEntryPoint类中有一个方法 commence()。 2. 定义SecurityAuthenticationEntryPoint类 我们定义一个SecurityAuthenticationEntryPoint类实现AuthenticationEntryPoint接口在这里负责输出未认证时的JSON结果。 /*** className: com.lison.springsecurity.config.security.handler- SecurityAuthenticationEntryPoint* description: 处理未登录认证时的响应信息* author: Lison* createDate: 2024-06-01*/ public class SecurityAuthenticationEntryPoint implements AuthenticationEntryPoint {Overridepublic void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {response.setContentType(application/json;charsetutf-8);PrintWriter out response.getWriter();out.write(尚未登录请先登录);out.flush();out.close();}}配置authenticationEntryPoint 然后我们在SecurityConfig配置类中调用authenticationEntryPoint()方法来关联上面定义的SecurityAuthenticationEntryPoint对象。 验证结果 配置完成后我们在未登录时直接去访问项目中的某个接口就会看到未登录时返回的JSON信息如下图所示:
http://www.w-s-a.com/news/860692/

相关文章:

  • 牡丹江市西安区建设局网站给公司做的东西放到自己网站上
  • 做网站的前景如何郑州seo规则
  • 学校户网站建设方案专业设计服务
  • 电子商务网站建设好么有一个网站怎么做cpc
  • 镇海住房和建设交通局网站跨境电商就是忽悠人的
  • 维修网站怎么做跨境电商发展现状如何
  • 手机网站设计公司皆选亿企邦桐乡市建设局官方网站
  • 企业培训 电子商务网站建设 图片山东省住房和城乡建设厅网站主页
  • 做酒招代理的网站赣icp南昌网站建设
  • 怎样做网站內链大连市建设工程信息网官网
  • 网站软件免费下载安装泰安网站建设收费标准
  • 部署iis网站校园网站设计毕业设计
  • 网站快慢由什么决定塘沽手机网站建设
  • 苏州那家公司做网站比较好装修队做网站
  • 外贸网站推广中山网站流量团队
  • 网站前端设计培训做一份网站的步zou
  • 网站备案拍照茶叶网页设计素材
  • wordpress 手机商城模板关键词优化软件有哪些
  • 网站301做排名python做的网站如何部署
  • 昆山做企业网站工信部网站 备案
  • 做英文的小说网站有哪些网站做qq登录
  • 湖州建设局招投标网站深圳广告公司集中在哪里
  • 重庆主城推广网站建设商城网站建设预算
  • 宁波品牌网站推广优化公司开发公司工程部工作总结
  • 长沙建站模板微信网站建设方案
  • 不让网站在手机怎么做门户网站 模板之家
  • 网站建设及推广图片wordpress文章摘要调用
  • 手机版网站案例全国信息企业公示系统
  • 模仿别人网站建设银行广州招聘网站
  • 沧州网站建设沧州内页优化