Spring Security基于json登錄實(shí)現(xiàn)過(guò)程詳解
主要是重寫(xiě)attemptAuthentication方法
導(dǎo)入依賴(lài)
<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=admin
spring.security.user.password=123
創(chuàng)建自定義身份過(guò)濾類(lèi)
寫(xiě)json登錄之前先看一下源碼,了解一下它是如何表單登錄的
在idea連按下shift鍵,搜索UsernamePasswordAuthenticationFilter類(lèi)
進(jìn)入后再按Ctrl+F12可以查看該類(lèi)的所有方法
進(jìn)入方法
我們只需要在request.getParameter()那里重寫(xiě)一下不就可以實(shí)現(xiàn)json登陸
重寫(xiě)attemptAuthentication(HttpServletRequestrequest,HttpServletResponseresponse)方法
只需要復(fù)制父類(lèi)的方法,多加一個(gè)判斷json的方法。就能同時(shí)支持key-value形式可json形式的參數(shù)了
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()); } //說(shuō)明是以json的形式傳遞參數(shù) if (request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE)) { String username = null; String password = null; //將傳入的json數(shù)據(jù)轉(zhuǎn)換成map再通過(guò)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配置類(lèi)
注:自定義的過(guò)濾類(lèi)和security原來(lái)那個(gè)表單登陸過(guò)濾設(shè)置是分開(kāi)的
體現(xiàn)在filter.setFilterProcessesUrl()和loginProcessingUrl
因此表單登陸和json登陸的,successHandler判斷也要分開(kāi)寫(xiě),
一會(huì)下面有效果圖也可以印證這一點(diǎn)
@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest().authenticated() .and() .formLogin() .loginProcessingUrl("/doLogin") .permitAll() .and() .csrf().disable(); //將自定義的過(guò)濾器加進(jìn)來(lái),第二參數(shù)表示加到usernamePasswordAuthenticationFilter所在的位置 http.addFilterAt(myAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class); } @Bean MyAuthenticationFilter myAuthenticationFilter() throws Exception{ MyAuthenticationFilter filter = new MyAuthenticationFilter(); filter.setAuthenticationManager(authenticationManagerBean()); return filter; } }
創(chuàng)建Controller
@RestController public class HelloController { @GetMapping("/hello") public String hello(){ return "hello security"; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java聊天室之實(shí)現(xiàn)使用Socket傳遞音頻
這篇文章主要為大家詳細(xì)介紹了Java簡(jiǎn)易聊天室之使用Socket實(shí)現(xiàn)傳遞音頻功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以了解一下2022-10-10Java執(zhí)行hadoop的基本操作實(shí)例代碼
這篇文章主要介紹了Java執(zhí)行hadoop的基本操作實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04將java中的 string 類(lèi)型轉(zhuǎn)成 數(shù)組案例
這篇文章主要介紹了將java中的 string 類(lèi)型轉(zhuǎn)成 數(shù)組案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09springboot中restful風(fēng)格請(qǐng)求的使用方法示例
RESTful是一種web軟件風(fēng)格,它不是標(biāo)準(zhǔn)也不是協(xié)議,它不一定要采用,只是一種風(fēng)格,它倡導(dǎo)的是一個(gè)資源定位(url)及資源操作的風(fēng)格,下面這篇文章主要給大家介紹了關(guān)于springboot中restful風(fēng)格請(qǐng)求的使用方法,需要的朋友可以參考下2023-02-02Java使用JDBC向MySQL數(shù)據(jù)庫(kù)批次插入10W條數(shù)據(jù)(測(cè)試效率)
使用JDBC連接MySQL數(shù)據(jù)庫(kù)進(jìn)行數(shù)據(jù)插入的時(shí)候,特別是大批量數(shù)據(jù)連續(xù)插入(100000),如何提高效率呢?今天小編通過(guò)本教程給大家介紹下2016-12-12SpringBoot啟動(dòng)指定profile的多種方式
這篇文章主要介紹了SpringBoot啟動(dòng)指定profile的多種方式,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09舉例詳解用Java實(shí)現(xiàn)web分頁(yè)功能的方法
這篇文章主要介紹了舉例詳解用Java實(shí)現(xiàn)web分頁(yè)功能的方法,這種基本功能現(xiàn)一般通過(guò)Hibernate框架來(lái)完成,需要的朋友可以參考下2015-10-10