日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot Oauth2安全性

發布時間:2023/12/3 javascript 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot Oauth2安全性 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這篇文章是對我以前的文章的增強,該文章討論了如何使用Spring security oauth2保護REST API。

萬一您錯過了它,可以在這里領取: http : //blog.rajithdelantha.com/2015/09/secure-your-rest-api-with-spring.html

Spring Boot是Spring框架的一項新發明,它使開發人員在構建大規模應用程序時的工作更加輕松。 這是抓住概念的好地方。

如果您查看我之前有關oauth2安全的文章,那么您知道在Spring端需要做一些配置。 但是另一方面,Spring boot將完成所有艱苦的工作,我們只需要通過簡單的注釋告訴他們該怎么做。

因此,本文是關于如何使用Spring安全性和Oauth2配置Spring引導項目的。 實際上,我們不能真正說出configure,因為所有大多數配置都是由Spring boot本身完成的。

  • 源代碼: https : //github.com/rajithd/spring-boot-oauth2

步驟1

對于這個項目,我在內存數據庫中使用H2。 因此,您無需在運行時創建任何數據庫和表。 但是,如果您希望該項目使用MySQL作為數據源,則首先創建數據庫,然后創建表。

CREATE TABLE user ( username VARCHAR(50) NOT NULL PRIMARY KEY, email VARCHAR(50), password VARCHAR(500), activated BOOLEAN DEFAULT FALSE, activationkey VARCHAR(50) DEFAULT NULL, resetpasswordkey VARCHAR(50) DEFAULT NULL ); CREATE TABLE authority ( name VARCHAR(50) NOT NULL PRIMARY KEY ); CREATE TABLE user_authority ( username VARCHAR(50) NOT NULL, authority VARCHAR(50) NOT NULL, FOREIGN KEY (username) REFERENCES user (username), FOREIGN KEY (authority) REFERENCES authority (name), UNIQUE INDEX user_authority_idx_1 (username, authority) ); CREATE TABLE oauth_access_token ( token_id VARCHAR(256) DEFAULT NULL, token BLOB, authentication_id VARCHAR(256) DEFAULT NULL, user_name VARCHAR(256) DEFAULT NULL, client_id VARCHAR(256) DEFAULT NULL, authentication BLOB, refresh_token VARCHAR(256) DEFAULT NULL ); CREATE TABLE oauth_refresh_token ( token_id VARCHAR(256) DEFAULT NULL, token BLOB, authentication BLOB );
  • 用戶表–系統用戶
  • 權威–角色
  • user_authority –用戶和角色的多對多表
  • oauth_access_token –存放access_token
  • oauth_refresh_token –保持refresh_token

添加一些種子數據。

INSERT INTO user (username,email, password, activated) VALUES ('admin', 'admin@mail.me', 'b8f57d6d6ec0a60dfe2e20182d4615b12e321cad9e2979e0b9f81e0d6eda78ad9b6dcfe53e4e22d1', true); INSERT INTO user (username,email, password, activated) VALUES ('user', 'user@mail.me', 'd6dfa9ff45e03b161e7f680f35d90d5ef51d243c2a8285aa7e11247bc2c92acde0c2bb626b1fac74', true); INSERT INTO user (username,email, password, activated) VALUES ('rajith', 'rajith@abc.com', 'd6dfa9ff45e03b161e7f680f35d90d5ef51d243c2a8285aa7e11247bc2c92acde0c2bb626b1fac74', true); INSERT INTO authority (name) VALUES ('ROLE_USER'); INSERT INTO authority (name) VALUES ('ROLE_ADMIN'); INSERT INTO user_authority (username,authority) VALUES ('rajith', 'ROLE_USER'); INSERT INTO user_authority (username,authority) VALUES ('user', 'ROLE_USER'); INSERT INTO user_authority (username,authority) VALUES ('admin', 'ROLE_USER'); INSERT INTO user_authority (username,authority) VALUES ('admin', 'ROLE_ADMIN');

第2步

配置WebSecurityAdapter

@Configuration @EnableWebSecurity public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Bean public PasswordEncoder passwordEncoder() { return new StandardPasswordEncoder(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .userDetailsService(userDetailsService) .passwordEncoder(passwordEncoder()); } @Override public void configure(WebSecurity web) throws Exception { web .ignoring() .antMatchers("/h2console/**") .antMatchers("/api/register") .antMatchers("/api/activate") .antMatchers("/api/lostpassword") .antMatchers("/api/resetpassword"); } @Override @Bean public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true) private static class GlobalSecurityConfiguration extends GlobalMethodSecurityConfiguration { @Override protected MethodSecurityExpressionHandler createExpressionHandler() { return new OAuth2MethodSecurityExpressionHandler(); } } }

第三步

Oauth2的配置

@Configuration public class OAuth2Configuration { @Configuration @EnableResourceServer protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter { @Autowired private CustomAuthenticationEntryPoint customAuthenticationEntryPoint; @Autowired private CustomLogoutSuccessHandler customLogoutSuccessHandler; @Override public void configure(HttpSecurity http) throws Exception { http .exceptionHandling() .authenticationEntryPoint(customAuthenticationEntryPoint) .and() .logout() .logoutUrl("/oauth/logout") .logoutSuccessHandler(customLogoutSuccessHandler) .and() .csrf() .requireCsrfProtectionMatcher(new AntPathRequestMatcher("/oauth/authorize")) .disable() .headers() .frameOptions().disable() .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .authorizeRequests() .antMatchers("/hello/**").permitAll() .antMatchers("/secure/**").authenticated(); } } @Configuration @EnableAuthorizationServer protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter implements EnvironmentAware { private static final String ENV_OAUTH = "authentication.oauth."; private static final String PROP_CLIENTID = "clientid"; private static final String PROP_SECRET = "secret"; private static final String PROP_TOKEN_VALIDITY_SECONDS = "tokenValidityInSeconds"; private RelaxedPropertyResolver propertyResolver; @Autowired private DataSource dataSource; @Bean public TokenStore tokenStore() { return new JdbcTokenStore(dataSource); } @Autowired @Qualifier("authenticationManagerBean") private AuthenticationManager authenticationManager; @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints .tokenStore(tokenStore()) .authenticationManager(authenticationManager); } @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients .inMemory() .withClient(propertyResolver.getProperty(PROP_CLIENTID)) .scopes("read", "write") .authorities(Authorities.ROLE_ADMIN.name(), Authorities.ROLE_USER.name()) .authorizedGrantTypes("password", "refresh_token") .secret(propertyResolver.getProperty(PROP_SECRET)) .accessTokenValiditySeconds(propertyResolver.getProperty(PROP_TOKEN_VALIDITY_SECONDS, Integer.class, 1800)); } @Override public void setEnvironment(Environment environment) { this.propertyResolver = new RelaxedPropertyResolver(environment, ENV_OAUTH); } } }

就是這個。 嘗試通過mvn spring-boot:run運行Spring Boot應用程序

然后通過執行以下curl檢查oauth2的安全性:

  • https://github.com/rajithd/spring-boot-oauth2

翻譯自: https://www.javacodegeeks.com/2015/10/spring-boot-oauth2-security.html

總結

以上是生活随笔為你收集整理的Spring Boot Oauth2安全性的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲熟女乱色一区二区三区 | 国产一区二区网址 | 草草久久久无码国产专区 | 免费成人深夜 | 超碰在线a| 后入内射欧美99二区视频 | 久久久91视频| 久久国产中文字幕 | 日本少妇videos高潮 | 最新中文字幕在线观看 | 公侵犯一区二区三区 | 曰韩av| 性高潮久久久久 | 激情小说中文字幕 | 欧美日韩精品亚洲精品 | 国精无码欧精品亚洲一区蜜桃 | 亚洲一区不卡在线 | 亚洲视频在线网 | 久久美利坚| 国产黄色小说 | av片免费| 好色成人网 | 国产成人在线精品 | 日日夜夜av | 国产夫妻露脸 | 欧美一级一级 | 国产白浆在线观看 | 成人私密视频 | av在线不卡网站 | 一区二区在线免费视频 | www.av天天 | 国产福利不卡视频 | www.欧美 | 一区二区91| 成人深夜福利视频 | 九九黄色片 | 久久精品一区 | 老司机深夜福利影院 | 国产精品美女在线 | 亚洲老妇色熟女老太 | 免费毛片看片 | 午夜剧场免费在线观看 | 91丨九色 | 麻豆一区产品精品蜜桃的特点 | 在线观看免费高清视频 | 免费激情小视频 | 成人免费精品 | 男人天堂999| 毛片天堂| 国内精品久久久久久久影视简单 | 免费成人深夜 | 永久av在线免费观看 | 欧美成人中文字幕 | 波多野结衣三级视频 | 成人漫画网站 | av导航网 | 丝袜美腿一区二区三区 | 骚色综合 | 国产crm系统91在线 | 91插视频| 国产精品久久麻豆 | 国产一区二区视频在线观看免费 | 少妇毛片视频 | 日韩少妇精品 | 欧美福利视频在线观看 | 韩毛片| 日本精品视频一区二区三区 | 亚洲激情中文字幕 | 久久午夜剧场 | 日韩三区在线 | 日韩插 | av日韩高清 | 伊人久久精品一区二区三区 | 久久美利坚 | 日韩精品一区二区视频 | 久操精品在线 | 成人免费大片黄在线播放 | 91禁国产网站 | 午夜视频色 | 播金莲一级淫片aaaaaaa | 老司机久久精品视频 | 亚洲精品色午夜无码专区日韩 | 天堂av.com | 中文字幕免费播放 | 五月婷婷综合久久 | 九九综合| www久久久com| 精品亚洲aⅴ无码一区二区三区 | 成人性生活视频 | 久久精品国产免费 | 一区二区手机在线 | 九色91av | 91大片在线观看 | 日韩一区久久 | 国产又黄又爽又色 | 久久尤物 | 探花精品 | free性欧美hd另类 | 精品一区二区三区四区五区 |