久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術(shù)文章
文章詳情頁

Spring Security基于json登錄實(shí)現(xiàn)過程詳解

瀏覽:56日期:2023-08-21 10:31:23

主要是重寫attemptAuthentication方法

導(dǎo)入依賴

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>

相關(guān)配置和代碼

application.properties配置密碼

spring.security.user.name=adminspring.security.user.password=123

創(chuàng)建自定義身份過濾類

寫json登錄之前先看一下源碼,了解一下它是如何表單登錄的

在idea連按下shift鍵,搜索UsernamePasswordAuthenticationFilter類

Spring Security基于json登錄實(shí)現(xiàn)過程詳解

進(jìn)入后再按Ctrl+F12可以查看該類的所有方法

Spring Security基于json登錄實(shí)現(xiàn)過程詳解

進(jìn)入方法

Spring Security基于json登錄實(shí)現(xiàn)過程詳解

我們只需要在request.getParameter()那里重寫一下不就可以實(shí)現(xiàn)json登陸

重寫attemptAuthentication(HttpServletRequestrequest,HttpServletResponseresponse)方法

只需要復(fù)制父類的方法,多加一個(gè)判斷json的方法。就能同時(shí)支持key-value形式可json形式的參數(shù)了

Spring Security基于json登錄實(shí)現(xiàn)過程詳解

public class MyAuthenticationFilter extends UsernamePasswordAuthenticationFilter { @Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { if(!request.getMethod().equals('POST')){ throw new AuthenticationServiceException('Authentication method not supported' + request.getMethod()); } //說明是以json的形式傳遞參數(shù) if (request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE)) { String username = null; String password = null; //將傳入的json數(shù)據(jù)轉(zhuǎn)換成map再通過get('key')獲得 try {Map<String,String> map =new ObjectMapper().readValue(request.getInputStream(), Map.class);username = map.get('username');password = map.get('password'); } catch (IOException e) {e.printStackTrace(); } if (username == null) { } if (password == null) { } username = username.trim(); UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password); setDetails(request, authRequest); return this.getAuthenticationManager().authenticate(authRequest); } return super.attemptAuthentication(request, response); }}

創(chuàng)建SecurityConfig配置類

Spring Security基于json登錄實(shí)現(xiàn)過程詳解

注:自定義的過濾類和security原來那個(gè)表單登陸過濾設(shè)置是分開的

體現(xiàn)在filter.setFilterProcessesUrl()和loginProcessingUrl

因此表單登陸和json登陸的,successHandler判斷也要分開寫,

一會(huì)下面有效果圖也可以印證這一點(diǎn)

@Configurationpublic class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().anyRequest().authenticated().and().formLogin().loginProcessingUrl('/doLogin').permitAll().and().csrf().disable(); //將自定義的過濾器加進(jìn)來,第二參數(shù)表示加到usernamePasswordAuthenticationFilter所在的位置 http.addFilterAt(myAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class); } @Bean MyAuthenticationFilter myAuthenticationFilter() throws Exception{ MyAuthenticationFilter filter = new MyAuthenticationFilter(); filter.setAuthenticationManager(authenticationManagerBean()); return filter; }}

創(chuàng)建Controller

@RestControllerpublic class HelloController { @GetMapping('/hello') public String hello(){ return 'hello security'; }}

Spring Security基于json登錄實(shí)現(xiàn)過程詳解

Spring Security基于json登錄實(shí)現(xiàn)過程詳解

Spring Security基于json登錄實(shí)現(xiàn)過程詳解

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国产欧美在线观看 | 不卡一区二区三区四区 | 婷婷色国产偷v国产偷v小说 | 免费一级在线观看 | 久久精品国产一区二区电影 | 日韩视频在线一区 | 精品影院 | 久久久久九九九九 | 欧美精品在线免费观看 | 91亚洲国产亚洲国产 | 大陆一级毛片免费视频观看 | 亚洲一区 | 夜夜超碰| 久久成人精品视频 | 亚洲精品一二三 | 91短视频版在线观看www免费 | 日韩在线视频免费 | 一区二区三区在线播放 | 亚洲日韩欧美一区二区在线 | 国产日韩一区二区三区 | 天天干天天av | 欧美成人精品一区二区男人看 | 国产一区二区三区四 | 91一区二区三区久久国产乱 | 日韩中文字幕av | 91精品一区二区 | 国产成人综合在线观看 | 欧美激情综合五月色丁香小说 | 日韩手机电影 | 欧美1级 | 国产一区 欧美 | 欧美午夜精品久久久 | 亚洲精品久久久一区二区三区 | 午夜视频91 | 欧美亚洲日本 | 成人精品一区二区三区中文字幕 | 在线一区二区三区做爰视频网站 | 免费视频一区二区三区在线观看 | 亚洲精品一 | 久久亚洲精品视频 | 成人免费大片黄在线播放 |