springboot快速搭建oauth2 內(nèi)存版身份認證

OAuth 2.0是用于授權(quán)的行業(yè)標準協(xié)議。OAuth 2.0取代了在2006年創(chuàng)建的原始OAuth協(xié)議上所做的工作并级。OAuth 2.0專注于客戶端開發(fā)人員的簡單性,同時為Web應(yīng)用程序届良,桌面應(yīng)用程序竟贯,手機和客廳設(shè)備提供特定的授權(quán)流程。

開發(fā)工具:Idea

開發(fā)環(huán)境:jdk1.8 甩栈、maven3.5.0

springboot 2.1.3

1泻仙、點擊【Create New Project】創(chuàng)建一個新的項目

選擇【Spring Initializr】選擇JDK1.8,然后點擊Next

接下來填好對應(yīng)的資料量没,點擊Next:

然后選擇Springboot2.1.3,然后勾選:

接下來點擊Next

點擊Finish開始項目的編寫

我們開始一個項目的時候玉转,首先要一步一步的去配置,不能一下盲目的去配置一大堆東西后去運行殴蹄,這樣如果出錯了就很難排錯究抓,我們一步步去配置所要的框架然后去運行沒問題了再去配置下一個。

我們先是安裝Thymeleaf

加入依賴:

<dependency>

? ? <groupId>org.springframework.boot</groupId>

? ? <artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

然后去resources目錄下面新建配置文件:application.yml

然后開始配置thymeleaf:

server:

? port: 8888

#thymeleaf

spring:

? thymeleaf:

? ? prefix: classpath:/templates/

? ? suffix: .html

? ? cache: false

? ? encoding: UTF-8

? ? mode: LEAGCYHTML5

? ? servlet:

? ? ? content-type: text/html

然后我們再去創(chuàng)建Controller包和ApiController.class類去編寫跳轉(zhuǎn)

package com.oauth2.pan.controller;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

/**

* @Author: Caiden(張志鑫)

* @Date: 2019/2/26 15:18

* @Version 0.0.1

*/

@Controller

@RequestMapping("/api")

public class ApiController {

? ? @RequestMapping("/hello")

? ? public ModelAndView sayHelloWorld(ModelAndView model){

? ? ? ? model.setViewName("index");

? ? ? ? return model;

? ? }

}

然后在resources -> templates 目錄下新建一個html文件:index.html袭灯。啟動運行測試:

可以訪問刺下,說明我們thymeleaf框架搭建好了。

此刻我們的目錄結(jié)構(gòu):

接下來搭建我們的oauth2認證稽荧,我們加入兩個依賴包

<dependency>

? ? <groupId>org.springframework.boot</groupId>

? ? <artifactId>spring-boot-starter-security</artifactId>

</dependency>

<dependency>

? ? <groupId>org.springframework.security.oauth</groupId>

? ? <artifactId>spring-security-oauth2</artifactId>

? ? <version>2.1.3.RELEASE</version>

</dependency>

需要注意的問題一:

1橘茉、當我們加入依賴包后可以在項目的External Libraries看下jar包是不是已經(jīng)被加入到項目里面,有些時候Jar已經(jīng)通過Maven下載完成,但是沒有導入進來捺癞。

2夷蚊、打開File->Project Structure -》Project Settings -> Libraries 中看oauth2的jar包是否有加載進來,如果下圖中綠色框框中是紅色髓介,請確保MAVEN下載JAR包成功的情況下惕鼓,刪除classes中的信息,再點擊綠色框上邊最左邊的“+”號唐础,選擇MAVEN下載好的jar包即可

當上面提到的兩個依賴jar包確認下載并加載成功后箱歧,開始進行認證編碼:

WebSecurityConfigurerAdapter是默認情況下Spring security的http配置;ResourceServerConfigurerAdapter是默認情況下spring security oauth 的http配置一膨。

(WebSecurityConfigurerAdapter的配置攔截要優(yōu)先于ResourceServerConfigurerAdapter呀邢,優(yōu)先級高的http配置是可以覆蓋優(yōu)先級低的配置的)

先覆寫spring Security的http配置

新建類名:WebSecurityConfiguration 然后去繼承 WebSecurityConfigurerAdapter

老司機避坑指南:解決 There is no PasswordEncoder mapped for the id "null")

SpringBoot升級到了2.0之后的版本,Security也由原來的版本4升級到了5

升到5版本之后的security必須加入密碼加密

WebSecurityConfiguration.class

package com.oauth2.pan.config;

import org.springframework.context.annotation.Bean;

import org.springframework.security.authentication.AuthenticationManager;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;

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.EnableWebSecurity;

import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import org.springframework.security.crypto.password.PasswordEncoder;

/**

* @Author: Caiden(張志鑫)

* @Date: 2019/2/26 15:57

* @Version 0.0.1

*/

@EnableGlobalMethodSecurity(prePostEnabled = true)

@EnableWebSecurity

public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

? ? @Override

? ? protected void configure(HttpSecurity http) throws Exception {

? ? ? ? http.csrf().disable();

? ? ? ? http.requestMatchers().antMatchers("/oauth/**","/login/**","/logout/**")

? ? ? ? ? ? ? ? .and()

? ? ? ? ? ? ? ? .authorizeRequests()

? ? ? ? ? ? ? ? .antMatchers("/oauth/**").authenticated()

? ? ? ? ? ? ? ? .and()

? ? ? ? ? ? ? ? .formLogin().permitAll();

? ? }

? ? @Override

? ? protected void configure(AuthenticationManagerBuilder auth) throws Exception {

? ? ? ? BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

? ? ? ? auth.inMemoryAuthentication().passwordEncoder(encoder)

? ? ? ? ? ? ? ? .withUser("admin")

? ? ? ? ? ? ? ? .password(encoder.encode("admin1234")).roles("USER");

? ? }

? ? /**

? ? * 需要配置這個支持password模式

? ? */

? ? @Override

? ? @Bean

? ? public AuthenticationManager authenticationManagerBean() throws Exception {

? ? ? ? return super.authenticationManagerBean();

? ? }

? ? /**

? ? * 密碼加密

? ? * */

? ? @Bean

? ? public PasswordEncoder passwordEncoder() {

? ? ? ? return new BCryptPasswordEncoder();

? ? }

}

再去覆寫Oauth2的http配置類

ResourceServerConfig.class

package com.oauth2.pan.config.oauth2;

import org.springframework.context.annotation.Configuration;

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;

/**

* @Author: Caiden(張志鑫)

* @Date: 2019/2/26 16:07

* @Version 0.0.1

*/

@Configuration

@EnableResourceServer

public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

? ? private static final String PAN_RESOURCE_ID = "*";

? ? @Override

? ? public void configure(HttpSecurity http) throws Exception {

? ? ? ? http.requestMatchers().antMatchers("/api/**")

? ? ? ? ? ? ? ? .and()

? ? ? ? ? ? ? ? .authorizeRequests()

? ? ? ? ? ? ? ? .antMatchers("/api/**").authenticated();

? ? }

? ? @Override

? ? public void configure(ResourceServerSecurityConfigurer resources) throws Exception {

? ? ? ? resources.resourceId(PAN_RESOURCE_ID);

? ? }

}

再去新建一個認證服務(wù)器:

AuthorizationServerConfiguration.class

package com.oauth2.pan.config.oauth2;

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.crypto.bcrypt.BCryptPasswordEncoder;

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.token.TokenStore;

import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;

/**

* @Author: Caiden(張志鑫)

* @Date: 2019/2/26 16:10

* @Version 0.0.1

*/

@Configuration

@EnableAuthorizationServer

public class AuthorizationServerConfiguration? extends AuthorizationServerConfigurerAdapter {

? ? @Autowired

? ? private TokenStore tokenStore;

? ? @Autowired

? ? private AuthenticationManager authenticationManager;

? ? @Override

? ? public void configure(AuthorizationServerSecurityConfigurer security)throws Exception {

? ? ? ? security.allowFormAuthenticationForClients();

? ? }

? ? @Override

? ? public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

? ? ? ? BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

? ? ? ? //添加客戶端信息

? ? ? ? //使用內(nèi)存存儲OAuth客戶端信息

? ? ? ? clients.inMemory()

? ? ? ? ? ? ? ? // client_id

? ? ? ? ? ? ? ? .withClient("test")

? ? ? ? ? ? ? ? // 該client允許的授權(quán)類型豹绪,不同的類型价淌,則獲得token的方式不一樣。

? ? ? ? ? ? ? ? .authorizedGrantTypes("password")

? ? ? ? ? ? ? ? // client_secret

? ? ? ? ? ? ? ? .secret(encoder.encode("123456"))

? ? ? ? ? ? ? ? .resourceIds("*")

? ? ? ? ? ? ? ? // 允許的授權(quán)范圍

? ? ? ? ? ? ? ? .scopes("all")

? ? ? ? ? ? ? ? .accessTokenValiditySeconds(1000) //token過期時間

? ? ? ? ? ? ? ? .refreshTokenValiditySeconds(1000); //refresh過期時間;

? ? }

? ? @Override

? ? public void configure(AuthorizationServerEndpointsConfigurer endpoints)throws Exception {

? ? ? ? //reuseRefreshTokens設(shè)置為false時瞒津,每次通過refresh_token獲得access_token時蝉衣,也會刷新refresh_token;也就是說巷蚪,會返回全新的access_token與refresh_token病毡。

? ? ? ? //默認值是true,只返回新的access_token屁柏,refresh_token不變啦膜。

? ? ? ? endpoints.tokenStore(tokenStore)

? ? ? ? ? ? ? ? .authenticationManager(authenticationManager);

? ? }

? ? @Bean

? ? public TokenStore tokenStore() {

? ? ? ? //token保存在內(nèi)存中(也可以保存在數(shù)據(jù)庫、Redis中)淌喻。

? ? ? ? //如果保存在中間件(數(shù)據(jù)庫僧家、Redis),那么資源服務(wù)器與認證服務(wù)器可以不在同一個工程中似嗤。

? ? ? ? //注意:如果不保存access_token啸臀,則沒法通過access_token取得用戶信息

? ? ? ? return new InMemoryTokenStore();

? ? }

}

好了届宠,都配置好了烁落,萬事OK,快去測試一下吧豌注。

關(guān)于分享有何疑問與建議歡迎留言哦伤塌!

如果你對Spring有興趣,實訓邦精心準備了Spring2.0的深度技術(shù)講解轧铁。

可以通過鏈接學習更多SpringBoot+Vue前后端分離的內(nèi)容每聪。

https://ke.qq.com/course/447591?flowToken=1013796

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子药薯,更是在濱河造成了極大的恐慌绑洛,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,544評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件童本,死亡現(xiàn)場離奇詭異真屯,居然都是意外死亡,警方通過查閱死者的電腦和手機穷娱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評論 3 392
  • 文/潘曉璐 我一進店門绑蔫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人泵额,你說我怎么就攤上這事配深。” “怎么了嫁盲?”我有些...
    開封第一講書人閱讀 162,764評論 0 353
  • 文/不壞的土叔 我叫張陵篓叶,是天一觀的道長。 經(jīng)常有香客問我羞秤,道長澜共,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,193評論 1 292
  • 正文 為了忘掉前任锥腻,我火速辦了婚禮嗦董,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘瘦黑。我一直安慰自己京革,他們只是感情好,可當我...
    茶點故事閱讀 67,216評論 6 388
  • 文/花漫 我一把揭開白布幸斥。 她就那樣靜靜地躺著匹摇,像睡著了一般。 火紅的嫁衣襯著肌膚如雪甲葬。 梳的紋絲不亂的頭發(fā)上廊勃,一...
    開封第一講書人閱讀 51,182評論 1 299
  • 那天,我揣著相機與錄音经窖,去河邊找鬼坡垫。 笑死,一個胖子當著我的面吹牛画侣,可吹牛的內(nèi)容都是我干的冰悠。 我是一名探鬼主播,決...
    沈念sama閱讀 40,063評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼配乱,長吁一口氣:“原來是場噩夢啊……” “哼溉卓!你這毒婦竟也來了皮迟?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,917評論 0 274
  • 序言:老撾萬榮一對情侶失蹤桑寨,失蹤者是張志新(化名)和其女友劉穎伏尼,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體尉尾,經(jīng)...
    沈念sama閱讀 45,329評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡烦粒,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,543評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了代赁。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片扰她。...
    茶點故事閱讀 39,722評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖芭碍,靈堂內(nèi)的尸體忽然破棺而出徒役,到底是詐尸還是另有隱情,我是刑警寧澤窖壕,帶...
    沈念sama閱讀 35,425評論 5 343
  • 正文 年R本政府宣布忧勿,位于F島的核電站,受9級特大地震影響瞻讽,放射性物質(zhì)發(fā)生泄漏鸳吸。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,019評論 3 326
  • 文/蒙蒙 一速勇、第九天 我趴在偏房一處隱蔽的房頂上張望晌砾。 院中可真熱鬧,春花似錦烦磁、人聲如沸养匈。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽呕乎。三九已至,卻和暖如春陨晶,著一層夾襖步出監(jiān)牢的瞬間猬仁,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評論 1 269
  • 我被黑心中介騙來泰國打工先誉, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留湿刽,地道東北人。 一個月前我還...
    沈念sama閱讀 47,729評論 2 368
  • 正文 我出身青樓谆膳,卻偏偏與公主長得像叭爱,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子漱病,可洞房花燭夜當晚...
    茶點故事閱讀 44,614評論 2 353

推薦閱讀更多精彩內(nèi)容