国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Spring Security基于json登錄實現過程詳解

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

主要是重寫attemptAuthentication方法

導入依賴

<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>

相關配置和代碼

application.properties配置密碼

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

創建自定義身份過濾類

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

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

Spring Security基于json登錄實現過程詳解

進入后再按Ctrl+F12可以查看該類的所有方法

Spring Security基于json登錄實現過程詳解

進入方法

Spring Security基于json登錄實現過程詳解

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

重寫attemptAuthentication(HttpServletRequestrequest,HttpServletResponseresponse)方法

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

Spring Security基于json登錄實現過程詳解

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的形式傳遞參數 if (request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE)) { String username = null; String password = null; //將傳入的json數據轉換成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); }}

創建SecurityConfig配置類

Spring Security基于json登錄實現過程詳解

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

體現在filter.setFilterProcessesUrl()和loginProcessingUrl

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

一會下面有效果圖也可以印證這一點

@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(); //將自定義的過濾器加進來,第二參數表示加到usernamePasswordAuthenticationFilter所在的位置 http.addFilterAt(myAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class); } @Bean MyAuthenticationFilter myAuthenticationFilter() throws Exception{ MyAuthenticationFilter filter = new MyAuthenticationFilter(); filter.setAuthenticationManager(authenticationManagerBean()); return filter; }}

創建Controller

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

Spring Security基于json登錄實現過程詳解

Spring Security基于json登錄實現過程詳解

Spring Security基于json登錄實現過程詳解

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 亚洲成人tv | 最新毛片久热97免费精品视频 | 91av久久| 免费看91毛片| 加勒比色| 中文国产日韩欧美视频 | 久久精品免费播放 | 波多野结衣视频在线观看 | 成人午夜在线观看国产 | 成人三级精品视频在线观看 | 日本一级全黄大片 | 久久国产成人午夜aⅴ影院 久久国产成人亚洲精品影院老金 | 欧美熟videos肥婆 | 日韩精品a | 男人在线网址 | 老司机免费福利午夜入口ae58 | 亚洲视频一区二区三区 | 看成年女人免费午夜视频 | 亚洲aa| 国产一区二区三区免费 | 97视频免费在线观看 | 亚洲自偷自偷图片在线高清 | 亚洲高清免费在线观看 | 国产精品亲子乱子伦xxxx裸 | 日韩精品欧美激情国产一区 | 久久99国产精品久久99 | 性感毛片 | 亚洲一区二区三区精品视频 | 久久久久女人精品毛片九一 | 美女张开腿让男人桶爽动漫视频 | 在线精品视频免费观看 | 久久国产视频一区 | 欧美成人午夜在线全部免费 | 在线成人aa在线看片 | 牛人国产偷窥女洗浴在线观看 | 99国产视频 | 国产综合久久久久影院 | 国产特黄特色一级特色大片 | 免费观看呢日本天堂视频 | 黄色片日本人 | 午夜两性视频免费看 |