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

手机一元云购网站建设软件开发和网站建设一样吗

手机一元云购网站建设,软件开发和网站建设一样吗,江小白采用的网络营销方式,做优化网站是什么意思上一篇 springboot 2.7 oauth server配置源码走读一中简单描述了oauth2 server的配置#xff0c;其中使用了内存保存 RegisteredClient#xff0c;本篇改用mysql存储。 db存储需要创建表#xff0c;表结构应该是什么样的呢#xff0c;从spring给我们封装好…上一篇 springboot 2.7 oauth server配置源码走读一中简单描述了oauth2 server的配置其中使用了内存保存 RegisteredClient本篇改用mysql存储。 db存储需要创建表表结构应该是什么样的呢从spring给我们封装好的源码入手 org.springframework.security.oauth2.server.authorization.client.JdbcRegisteredClientRepository类中 那么字段类型呢我们看org.springframework.security.oauth2.server.authorization.client.RegisteredClient类 解释下为什么时间用timestamp存储以及Set数据模型用字符串存储 解释下为什么ClientSettings和TokenSettings用json存储当然varchar也行就是一个map. 至此咱们确定了表结构如下是一个示例 CREATE TABLE oauth2_registered_client (id varchar(36) COLLATE utf8mb4_general_ci NOT NULL,client_id varchar(100) COLLATE utf8mb4_general_ci NOT NULL,client_id_issued_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,client_secret varchar(100) COLLATE utf8mb4_general_ci DEFAULT ,client_secret_expires_at timestamp NULL DEFAULT NULL,client_name varchar(50) COLLATE utf8mb4_general_ci NOT NULL,client_authentication_methods varchar(100) COLLATE utf8mb4_general_ci NOT NULL,authorization_grant_types varchar(100) COLLATE utf8mb4_general_ci NOT NULL,redirect_uris varchar(100) COLLATE utf8mb4_general_ci DEFAULT ,scopes varchar(200) COLLATE utf8mb4_general_ci NOT NULL,client_settings json NOT NULL,token_settings json NOT NULL,PRIMARY KEY (id) ) ENGINEInnoDB DEFAULT CHARSETutf8mb4 COLLATEutf8mb4_general_ci;创建好表后咱们处理代码 1.在pom中引入依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-jdbc/artifactId /dependency dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.26/version /dependency2.yaml/properties配置文件中添加数据库信息示例如下 spring:datasource:url: jdbc:mysql://localhost:3306/db?useUnicodetruecharacterEncodingutf-8serverTimezoneAsia/ShanghaiuseSSLfalseusername: usernamepassword: password3.使用JdbcRegisteredClientRepository它和InMemoryRegisteredClientRepository只能二选一所以需要注释掉后者。在咱们自己的配置类OAuth2AuthorizeSecurityConfig中 Beanpublic RegisteredClientRepository registeredClientRepository(JdbcTemplate jdbcTemplate) {return new JdbcRegisteredClientRepository(jdbcTemplate);}4.初始化数据到db表中写一个测试类方法插入数据 package com.jel.tech.auth;import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.security.oauth2.core.AuthorizationGrantType; import org.springframework.security.oauth2.core.ClientAuthenticationMethod; import org.springframework.security.oauth2.core.oidc.OidcScopes; import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; import org.springframework.security.oauth2.server.authorization.config.ClientSettings; import org.springframework.security.oauth2.server.authorization.config.TokenSettings;import javax.annotation.Resource; import java.time.Duration; import java.util.UUID;SpringBootTest class AuthApplicationTests {Resourceprivate RegisteredClientRepository registeredClientRepository;Testvoid saveRegisteredClients() {RegisteredClient loginClient RegisteredClient.withId(UUID.randomUUID().toString()).clientId(login-client).clientSecret({noop}openid-connect).clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC).authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE).authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN).redirectUri(http://127.0.0.1:8080/login/oauth2/code/login-client).redirectUri(http://127.0.0.1:8080/authorized).scope(OidcScopes.OPENID).scope(OidcScopes.PROFILE).clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build()).build();RegisteredClient registeredClient RegisteredClient.withId(UUID.randomUUID().toString()).clientId(messaging-client).clientSecret({noop}secret).clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC).authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS).scope(message:read).scope(message:write)// 指定token有效期token:30分默认5分钟,refresh_token:1天.tokenSettings(TokenSettings.builder().accessTokenTimeToLive(Duration.ofMinutes(30)).refreshTokenTimeToLive(Duration.ofDays(1)).build()).build();// 注意没有设置clientName,则会把id值作为clientNameregisteredClientRepository.save(loginClient);registeredClientRepository.save(registeredClient);} }5.验证功能在此我借花献佛把官网提供的示例复制过来 package com.jel.tech.auth;import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.HttpHeaders; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.request.RequestPostProcessor; import org.springframework.test.web.servlet.result.MockMvcResultHandlers;import java.util.Map;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;/*** Integration tests for {link AuthApplication}.** author Steve Riesenberg*/ SpringBootTest AutoConfigureMockMvc ActiveProfiles(test) public class OAuth2AuthorizationServerApplicationITests {private static final String CLIENT_ID messaging-client;private static final String CLIENT_SECRET secret;private final ObjectMapper objectMapper new ObjectMapper();Autowiredprivate MockMvc mockMvc;Testvoid performTokenRequestWhenValidClientCredentialsThenOk() throws Exception {// formatter:offthis.mockMvc.perform(post(/oauth2/token).param(grant_type, client_credentials).param(scope, message:read).with(basicAuth(CLIENT_ID, CLIENT_SECRET))).andExpect(status().isOk()).andExpect(jsonPath($.access_token).isString()).andExpect(jsonPath($.expires_in).isNumber()).andExpect(jsonPath($.scope).value(message:read)).andExpect(jsonPath($.token_type).value(Bearer));// formatter:on}Testvoid performTokenRequestWhenMissingScopeThenOk() throws Exception {// formatter:offthis.mockMvc.perform(post(/oauth2/token).param(grant_type, client_credentials).with(basicAuth(CLIENT_ID, CLIENT_SECRET))).andExpect(status().isOk()).andExpect(jsonPath($.access_token).isString()).andExpect(jsonPath($.expires_in).isNumber()).andExpect(jsonPath($.scope).value(message:read message:write)).andExpect(jsonPath($.token_type).value(Bearer));// formatter:on}Testvoid performTokenRequestWhenInvalidClientCredentialsThenUnauthorized() throws Exception {// formatter:offthis.mockMvc.perform(post(/oauth2/token).param(grant_type, client_credentials).param(scope, message:read).with(basicAuth(bad, password))).andExpect(status().isUnauthorized()).andExpect(jsonPath($.error).value(invalid_client));// formatter:on}Testvoid performTokenRequestWhenMissingGrantTypeThenUnauthorized() throws Exception {// formatter:offthis.mockMvc.perform(post(/oauth2/token).with(basicAuth(bad, password))).andExpect(status().isUnauthorized()).andExpect(jsonPath($.error).value(invalid_client));// formatter:on}Testvoid performTokenRequestWhenGrantTypeNotRegisteredThenBadRequest() throws Exception {// formatter:offthis.mockMvc.perform(post(/oauth2/token).param(grant_type, client_credentials).with(basicAuth(login-client, openid-connect))).andExpect(status().isBadRequest()).andExpect(jsonPath($.error).value(unauthorized_client));// formatter:on}Testvoid performIntrospectionRequestWhenValidTokenThenOk() throws Exception {// formatter:offthis.mockMvc.perform(post(/oauth2/introspect).param(token, getAccessToken()).with(basicAuth(CLIENT_ID, CLIENT_SECRET))).andExpect(status().isOk()).andExpect(jsonPath($.active).value(true)).andExpect(jsonPath($.aud[0]).value(CLIENT_ID)).andExpect(jsonPath($.client_id).value(CLIENT_ID)).andExpect(jsonPath($.exp).isNumber()).andExpect(jsonPath($.iat).isNumber()).andExpect(jsonPath($.iss).value(http://127.0.0.1:9000)).andExpect(jsonPath($.nbf).isNumber()).andExpect(jsonPath($.scope).value(message:read)).andExpect(jsonPath($.sub).value(CLIENT_ID)).andExpect(jsonPath($.token_type).value(Bearer)).andDo(MockMvcResultHandlers.print());// formatter:on}Testvoid performIntrospectionRequestWhenInvalidCredentialsThenUnauthorized() throws Exception {// formatter:offthis.mockMvc.perform(post(/oauth2/introspect).param(token, getAccessToken()).with(basicAuth(bad, password))).andExpect(status().isUnauthorized()).andExpect(jsonPath($.error).value(invalid_client));// formatter:on}private String getAccessToken() throws Exception {// formatter:offMvcResult mvcResult this.mockMvc.perform(post(/oauth2/token).param(grant_type, client_credentials).param(scope, message:read).with(basicAuth(CLIENT_ID, CLIENT_SECRET))).andExpect(status().isOk()).andExpect(jsonPath($.access_token).exists()).andReturn();// formatter:onString tokenResponseJson mvcResult.getResponse().getContentAsString();MapString, Object tokenResponse this.objectMapper.readValue(tokenResponseJson, new TypeReferenceMapString, Object() {});String access_token tokenResponse.get(access_token).toString();System.out.println(access_token);return access_token;}private static BasicAuthenticationRequestPostProcessor basicAuth(String username, String password) {return new BasicAuthenticationRequestPostProcessor(username, password);}private static final class BasicAuthenticationRequestPostProcessor implements RequestPostProcessor {private final String username;private final String password;private BasicAuthenticationRequestPostProcessor(String username, String password) {this.username username;this.password password;}Overridepublic MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) {HttpHeaders headers new HttpHeaders();headers.setBasicAuth(this.username, this.password);request.addHeader(Authorization, headers.getFirst(Authorization));return request;}} }
http://www.w-s-a.com/news/881977/

相关文章:

  • 海外医疗兼职网站建设公司取名字大全免费
  • 龙口市规划建设局网站vi设计和品牌设计的区别
  • 企业网站的总体设计网站建设评审验收会议主持词
  • 网站建设完成推广响应式网站设计开发
  • 电商网站用php做的吗网站开发流程可规划为那三个阶段
  • flash网站怎么做音乐停止深圳网站建设金瓷网络
  • 哪个网站可以做房产信息群发怎么做国内网站吗
  • 微商城网站建设公司的价格卖磁铁的网站怎么做的
  • 免费做做网站手机平台软件开发
  • 网站单页做301徐州百度网站快速优化
  • 织梦怎么制作手机网站漳州专业网站建设公司
  • 邓州做网站网络优化概念
  • 查看网站开发phonegap wordpress
  • 网站建设和维护待遇怎样c 做的网站又哪些
  • 淮南网站推广网站开发行业前景
  • 丽水市龙泉市网站建设公司江门手机模板建站
  • 做化妆品注册和注册的网站有哪些wordpress加关键字
  • 四川新站优化php笑话网站源码
  • 外贸类网站酷玛网站建设
  • 合肥网站设计建设南宁网站seo推广优化公司
  • 临沂百度网站7x7x7x7x8黄全场免费
  • 海洋牧场网站建设大良网站设计价格
  • 手机端网站关键字排名北京seo公司哪家好
  • 福建建设培训中心网站网站建站服务公司地址
  • 青岛网站优化快速排名企业网址怎么整
  • 做公司网站用什么系统seo搜索排名优化方法
  • dw怎么做网站标题图标做网站重庆
  • 机场建设相关网站公司官网设计制作
  • 大学网站建设的目标技术支持 优府网络太原网站建设
  • wordpress设置密码访问带提示广州做网站优化哪家专业