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

合肥 网站制作河南网站seo优化

合肥 网站制作,河南网站seo优化,健康河北app下载二维码,ui和平面设计的区别目录 前言 1.介绍 2.Oauth2.0过程详解 3.Oauth 整合到 Spring Boot 实践 4.方法及配置详解#xff1a; 总结 前言 Oauth2.0 是非常流行的网络授权表准#xff0c;已经广泛应用在全球范围内#xff0c;比较大的公司#xff0c;如腾讯等都有大量的应用场景。 1.介绍 …目录 前言 1.介绍 2.Oauth2.0过程详解  3.Oauth 整合到 Spring Boot 实践  4.方法及配置详解 总结 前言 Oauth2.0 是非常流行的网络授权表准已经广泛应用在全球范围内比较大的公司如腾讯等都有大量的应用场景。 1.介绍 Oauth2.0 全称是 Open Authorization是一种开放授权协议。目前使用的版本是 2.0 版本也就是 Oauth2.0它主要用于授权认证环节。 从官网文档可知道 Oauth 具有如下特点 需要第三方进行授权会存储用户登录授权凭据。服务器必须支持密码认证。有限范围内获取所有者(用户)的部分数据。  简而言之就是用于第三方在用户授权的前提下获取用户的相关信息。而 Oauth2.0 的授权模式比较多常用的有如下四种 授权码模式最常规的模式适用于有服务器端的第三方应用如网页应用。在这种模式下用户代理如浏览器首先被重定向到授权服务器进行身份验证授权服务器通过用户代理回调客户端并提供一个授权码。客户端随后会使用这个授权码向授权服务器后端请求访问令牌access token支持刷新的 token。Clinet 模式适用于客户端应用直接访问它们自己的资源服务器没有用户直接参与的场景。在这个模式中客户端应用凭借自己的凭据客户端ID及客户端密钥向授权服务器请求访问令牌。其他应用或者程序通过 api 进行调用获取对应的 token。简化模式用于没有服务器端能力的客户端应用如 JavaScript 应用。在简化模式中客户端通过用户代理直接从授权服务器获取访问令牌不通过中间的授权码交换步骤。由于这种模式暴露了访问令牌因此不如授权码模式安全。密码模式 如果用户对第三方应用高度信任例如设备操作系统或具备原生应用的第三方服务则可以直接将用户名和密码提供给应用应用使用这些凭据直接从授权服务器获取访问令牌。由于此流程涉及明文传递用户凭据因此安全性较低。 总结从中其实可以看出最关键的部分就是这三方相互确认的过程。授权码模式就可以看成三方之间都不信任所以需要不断地相互确认而简化模式则可以看作授权服务器对第三方应用比较信任表示直接就给了你我的令牌你并不会拿我的令牌去做坏事也就是说它们之间比较互信密码模式则是资源拥有者对第三方应用比较信任可以将自己的信息放心的放给第三方客户端模式则可以看成三方都比较信任不需要过多的验证请求就给令牌就行了比较适合我们内部的应用场景。 梳理 Oauth2.0 中有一个主线就是三方参与者之间的信任程度。 2.Oauth2.0过程详解  前面我们对四个常见的模式进行了详细的了解为了更加方便理解对基本的过程再次进行详细解释。Oauth 的基本认正过程如图分为三个角色用户、第三方、以及认证服务器。 首先用户去访问第三方应用第三方应用引导用户进行授权跳转到授权页面。用户进行授权后将数据传递给认证服务器认证服务器返回 code 给第三方应用第三方应用发起新的请求来获取访问授权令牌(access token)最后用户获取到授权结果。  3.Oauth 整合到 Spring Boot 实践  客户端凭证是最常用的认证方式之一。例如微信授权就是这种方式通过携带 access token 来获取用户资源。 新建Spring Boot 项目实现方案是使用 SpringSecurity 和 Oauth2.0 模块pom.xml 添加如下依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-security/artifactId/dependencydependencygroupIdorg.springframework.security.oauth/groupIdartifactIdspring-security-oauth2/artifactIdversion2.3.5.RELEASE/version/dependency 为了实现 Oauth 认证需要对两方面进行配置一是认证服务配置包含 token 定义用户客户端的信息以及授权服务和令牌服务。二是需要对资源服务进行配置如资源访问权限设置哪些需要 token 验证。 首先编写认证服务配置定义授权以及令牌服务。 package org.example.config;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.NoOpPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer; import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer; import org.springframework.security.oauth2.provider.ClientDetailsService; import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices; import org.springframework.security.oauth2.provider.code.InMemoryAuthorizationCodeServices; import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices; import org.springframework.security.oauth2.provider.token.DefaultTokenServices; import org.springframework.security.oauth2.provider.token.TokenStore; import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore; import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore; import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;/*** 认证服务器类** author freejava*/ Configuration //此注解不像其他的 Enable注解一样内部有 Component 注解 EnableAuthorizationServer public class MySuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {Autowiredprivate AuthenticationManager authenticationManager;Autowiredprivate AuthorizationCodeServices authorizationCodeServices;//令牌访问安全策略Overridepublic void configure(AuthorizationServerSecurityConfigurer security) throws Exception {//用于表单方式提交 client_id,client_secretsecurity.tokenKeyAccess(permitAll()).checkTokenAccess(permitAll()).allowFormAuthenticationForClients();}Beanpublic PasswordEncoder noOpPasswordEncoder(){return NoOpPasswordEncoder.getInstance();}/*** 配置客户端信息 哪些客户端可以发送请求* param clients 定义客户端信息的配置们可以初始化客户端信息。* throws Exception*/Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {clients.inMemory()//client_id.withClient(myClientId)// client_secret.secret(NoOpPasswordEncoder.getInstance().encode(123456))//授权方式.authorizedGrantTypes(authorization_code, password,client_credentials,implicit,refresh_token).resourceIds(res1)//可以访问哪些资源//授权范围.scopes(all)//write.autoApprove(false)//false跳转到授权页面//加上验证回调地址.redirectUris(http://www.baidu.com);}Autowiredprivate ClientDetailsService clientDetailsService;// 令牌服务Beanpublic AuthorizationServerTokenServices tokenService(){DefaultTokenServices services new DefaultTokenServices();services.setClientDetailsService(clientDetailsService);services.setSupportRefreshToken(true);services.setTokenStore(tokenStore());services.setAccessTokenValiditySeconds(7200);//2小时services.setRefreshTokenValiditySeconds(259200);//刷新令牌默认有效期 3 天return services;}//设置授权码模式如何存储Beanpublic AuthorizationCodeServices authorizationCodeServices() {return new InMemoryAuthorizationCodeServices();}private String SIGNING_KEY uaa123;//内存方式Beanpublic TokenStore tokenStore(){return new InMemoryTokenStore();//new JwtTokenStore(accessTokenConverter());}/* Beanpublic JwtAccessTokenConverter accessTokenConverter() {JwtAccessTokenConverter converter new JwtAccessTokenConverter();converter.setSigningKey(SIGNING_KEY); //对称秘钥资源服务器使用该秘钥来验证return converter;}*//*** 定义授权和令牌服务* param endpoints* throws Exception*/Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {//super.configure(endpoints);endpoints.authenticationManager(authenticationManager).authorizationCodeServices(authorizationCodeServices)//授权码服务.tokenServices(tokenService())//令牌管理服务.allowedTokenEndpointRequestMethods(HttpMethod.POST,HttpMethod.GET);//允许POST提交} }资源配置编写先编写一个 Controller 文件代码如下 package org.example.controller;import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController;RestController public class ResController {GetMapping(/res/{id})public String testOauth(PathVariable String id) {return Get the resource id;} }然后编写该资源的访问权限配置代码如下 package org.example.config;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer; import org.springframework.security.oauth2.provider.token.TokenStore;/*** 用于拦截请求的配置类*/ Configuration EnableResourceServer public class MyResourceServerConfigurer extends ResourceServerConfigurerAdapter {Autowiredprivate TokenStore tokenStore;Overridepublic void configure(ResourceServerSecurityConfigurer resources) throws Exception {//这里重点注意resources.tokenStore(tokenStore).resourceId(res1);}/*** 用于拦截 http 请求* param http* throws Exception*/Overridepublic void configure(HttpSecurity http) throws Exception {http.csrf().disable().authorizeRequests().antMatchers(/oauth/**).permitAll() // 允许所有人访问 /oauth/* 下面的内容。.anyRequest().authenticated(); // 其他所有请求都需要认证。/*http.authorizeRequests().anyRequest().authenticated();//(/res/**).authenticated();*/} }为授权码模式编写一个webconfig package org.example.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;/*** author Administrator* version 1.0**/ Configuration EnableGlobalMethodSecurity(securedEnabled true,prePostEnabled true) public class WebSecurityConfig extends WebSecurityConfigurerAdapter {//认证管理器Beanpublic AuthenticationManager authenticationManagerBean() throws Exception {return super.authenticationManagerBean();}//安全拦截机制最重要Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().disable().authorizeRequests().antMatchers(/orther/**).hasAnyAuthority(p1).antMatchers(/login*).permitAll().anyRequest().authenticated().and().formLogin();} }通过 Postman 进行测试访问 localhost:8080/res/1 会返回一个 unauthorized 的错误返回这里需要传递 access token所以需要先请求获取 access token 的接口 /oauth/token之后再用该token进行请求即可。  4.方法及配置详解 ClientDetailsServiceConfigurer 详解  ClientDetailsServiceConfigurer 是用于配置客户端详细信息服务的类。客户端详细信息服务定义了客户端应用程序的信息这包括客户端ID、客户端密钥、授权模式、令牌有效期以及重定向的URI等信息。客户端详细信息可以通过硬编码、数据库等方式来存储和管理。 以下是 ClientDetailsServiceConfigurer 的一些常用方法 inMemory() 在内存中配置客户端存储主要用于测试或简单应用程序不适合生产使用。 jdbc(DataSource dataSource) 使用 JDBC 连接数据库并在数据库中维护客户端信息。 withClient(String clientId) 创建新的客户端并设置其客户端id。 secret(String secret) 设置客户端的密钥。对于使用Spring Security 5的应用程序需要注意密码存储格式通常是 {bcrypt}、{noop} 等。 redirectUris(String… redirectUris) 设置客户端的重定向URI这些URI在OAuth2授权码模式下使用。 authorizedGrantTypes(String… authorizedGrantTypes) 设置客户端允许的授权模式如 authorization_code, password, refresh_token, client_credentials。 authorities(String… authorities) 为客户端设置Spring Security的权限。 scopes(String… scopes) 设置客户端的作用域(scope)限定客户端的访问范围。 accessTokenValiditySeconds(int accessTokenValiditySeconds) 设置访问令牌的有效时间以秒计。 refreshTokenValiditySeconds(int refreshTokenValiditySeconds) 设置刷新令牌的有效时间以秒计。 additionalInformation(MapString, ? additionalInformation) 设置一个包含其他客户端详细信息的Map。 autoApprove(boolean autoApprove) 如果设置为true则不需要用户在授权阶段进行手动批准。 autoApprove(String… scopes) 指定特定范围的自动批准。 resourceIds(String… resourceIds) 限定客户端可以访问的资源服务器的ID。  AuthorizationServerEndpointsConfigurer详解 默认得到访问路径 /oauth/authorize: 授权端点/oauth/token : 令牌端点。/loauth/confirm_access : 用户确认授权提交端点。/loauth/error: 授权服务错误信息端点。/oauth/check_token : 用于资源服务访问的令牌解析端点/oauth/token_key : 提供公有密匙的端点如果你使用JWT令牌的话.  AuthorizationServerEndpointsConfigurer是Spring Security OAuth2中用于定义授权服务器端点配置的类它允许你定义与认证相关的各种参数和实现。也就是配置令牌的访问端点发放令牌的地址和令牌服务如何发放下面是一些 AuthorizationServerEndpointsConfigurer 的主要配置方法以及它们的用途 authenticationManager(AuthenticationManager authenticationManager) 设置认证管理器。在password授权模式下需要配置此项来验证用户的用户名和密码。 userDetailsService(UserDetailsService userDetailsService) 设置用户细节服务。如果你设置了一个自定义的用户细节服务可以通过这个方法来配置。 authorizationCodeServices(AuthorizationCodeServices authorizationCodeServices) 设置授权码服务。用于authorization_code授权类型。这能够改变默认的授权码存储方式通常是内存。 tokenStore(TokenStore tokenStore) 设置令牌存储方式。可以使用各种不同的存储方式如内存、JDBC或JWT tokens。 accessTokenConverter(AccessTokenConverter accessTokenConverter) 设置访问令牌转换器。例如使用JWT token时可以配置一个JwtAccessTokenConverter。 tokenEnhancer(TokenEnhancer tokenEnhancer) 设置令牌增强器。可以用来扩展JWT token内容。 approvalStore(ApprovalStore approvalStore) 设置批准存储。这跟token的保存方式类似定义了用户批准记录的存储方式。 reuseRefreshTokens(boolean reuseRefreshTokens) 设置是否复用refresh tokens。默认为true即当新的access token被创建后原始的refresh token将会被复用。 refreshTokenGranter(TokenGranter refreshTokenGranter) 设置自定义的refresh token授权者。 grantTypes(TokenGranter tokenGranter) 设置自定义授权类型可以添加或覆盖默认的授权类型来扩展功能。 tokenValidator(OAuth2TokenValidatorJwt tokenValidator) 在使用JWT时设置令牌验证器用以验证JWT的有效性。 tokenServices(DefaultTokenServices tokenServices) 设置token服务的实例。这个服务负责创建和管理OAuth2 tokens。 exceptionTranslator(WebResponseExceptionTranslatorOAuth2Exception exceptionTranslator) 设置异常翻译器。用来定义认证异常的响应格式。 requestFactory(OAuth2RequestFactory requestFactory) 设置OAuth2请求工厂。这允许你插入自定义逻辑来解析传入的请求。  AuthorizationServerSecurityConfigurer 用来配置令牌端点的安全约束比如哪些人可以访问。 ResourceServerSecurityConfigurer详解 ResourceServerSecurityConfigurer 是 Spring Security OAuth2 中用于配置资源服务器的安全细节的一个类。它允许你设置一些关于资源服务器的关键配置比如 tokenStore、resourceId 和 tokenServices 等。下面是一些 ResourceServerSecurityConfigurer 的常用方法及其简要说明 tokenStore(TokenStore tokenStore) 设置用于读取令牌信息的 TokenStore。这个 TokenStore 被资源服务器用来解码访问令牌。常见的实现是 JdbcTokenStore、JwtTokenStore 和 InMemoryTokenStore。 resourceId(String resourceId) 设置此资源服务器的资源ID。这通常用于客户端请求的 Scope 限制以及在多个资源服务器存在时加以区分。 tokenServices(TokenServices tokenServices) 设置 ResourceServerTokenServices 实例这是用来解码访问令牌的另一种方式。如果你的令牌是 JWT则可以提供 JwtTokenServices。 tokenExtractor(TokenExtractor tokenExtractor) 设置用于提取访问令牌的 TokenExtractor。默认情况下Spring OAuth2 使用 BearerTokenExtractor 来处理来自请求头或请求参数的 Bearer 令牌。 authenticationEntryPoint(AuthenticationEntryPoint authenticationEntryPoint) 定制用于处理认证错误的 AuthenticationEntryPoint。通常情况下这用于处理资源服务器返回给客户端的401响应。 accessDeniedHandler(AccessDeniedHandler accessDeniedHandler) 设置自定义 AccessDeniedHandler 以处理接收到访问被拒绝时的情况。这通常用来自定义服务器对403 Forbidden响应的处理。 stateless(Boolean stateless) 设置此资源服务器是否只关心无状态请求应答。这确定了资源服务器是否会在请求间创建 HttpSession。 authenticationManager(AuthenticationManager authenticationManager) 设置 AuthenticationManager。如果需要可以用来登陆用户或验证用户的访问令牌。 eventPublisher(ApplicationEventPublisher eventPublisher) 设置 ApplicationEventPublisher 以发布认证事件。这可以用来记录认证成功或者失败等事件。 总结 OAuth 2.0 是一个用于授权的开放标准由 IETF OAuth 工作组于 2012 年发布。它允许用户授权第三方应用访问其在某一服务上的信息而无需将用户名和密码提供给第三方应用大大促进了当前互联网间的信息共享。
http://www.zqtcl.cn/news/652700/

相关文章:

  • 先建设网站后付款网站相对路径和绝对路径
  • 临沂外贸国际网站建设网站开发外包公司合同
  • 网站设置快捷方式温州网站建设方案报价
  • 经营网站需要什么费用如何鉴赏网站论文
  • 聊城网站推广公司网站 防攻击
  • 小米盒子做网站一个县城广告公司利润
  • 天津市区县档案部门网站建设指导意见网站开发的需求分析教学视频
  • 网站服务合同范本企业网站建设费是无形资产吗
  • 国外做家纺的网站试用体验网站
  • 百度网站下载安装免费制作短视频的软件
  • 山西省这房和城乡建设厅网站邯郸北京网站建设
  • 廊坊网站seo服务主机服务器网站 怎么做
  • 网站的建设与运维东营会计信息网
  • 郑州网站建设程序3g手机网站
  • 建设监理网站设计了网站首页
  • 织梦教育网站开发商务网站建设实训总结
  • 广西执业药师培训网站网站设计 原型图
  • 网站建设客户群体分析微信开放平台小程序开发文档
  • led网站建设wordpress .htaccess 固定链接
  • 学校网站建设申请报告一个好网站设计
  • 网站雪花特效wordpress文件解析
  • 招聘网站哪个好用淮北之窗
  • 索莱宝做网站网站在线布局
  • 站内seo的技巧做php网站阿里云服务器
  • 网站开发需要用到哪些软件爱站网权重查询
  • 免费注册个人网站铁路工程造价信息网
  • 电子商务大型网站建设电商静态网页模板
  • 网站建设公司利润怎么样长沙网站制作作
  • 淄博优化网站企业营销型网站做的好
  • 玉泉营网站建设网络营销公司组织架构