国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring Security基于json登錄實現(xiàn)過程詳解

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

2020-08-17 12:09柒丶月 Java教程

這篇文章主要介紹了Spring Security基于json登錄實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

主要是重寫attemptAuthentication方法

導入依賴

?
1
2
3
4
5
6
7
8
<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=admin
spring.security.user.password=123

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

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

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

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

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

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

進入方法

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

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

重寫attemptAuthentication(HttpServletRequestrequest,HttpServletResponseresponse)方法

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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ù)據轉換成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登錄實現(xiàn)過程詳解

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

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@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();
    //將自定義的過濾器加進來,第二參數(shù)表示加到usernamePasswordAuthenticationFilter所在的位置
    http.addFilterAt(myAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
  }
 
  @Bean
  MyAuthenticationFilter myAuthenticationFilter() throws Exception{
    MyAuthenticationFilter filter = new MyAuthenticationFilter();
    filter.setAuthenticationManager(authenticationManagerBean());
    return filter;
 
  }
}

創(chuàng)建Controller

?
1
2
3
4
5
6
7
@RestController
public class HelloController {
  @GetMapping("/hello")
  public String hello(){
    return "hello security";
  }
}

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

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

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/qiuwenli/p/13447061.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美成人精品一区二区三区 | 精品国产乱码久久久久久久软件 | 一级黄色国产片 | 91视频网 | 成人在线一区二区 | 欧美视频免费看 | 中文字幕高清一区 | 国产精品久久久久久婷婷天堂 | 欧美日韩视频第一页 | 日韩中文在线视频 | 福利片在线观看 | 中文日韩在线 | 日本人在线观看 | 亚洲一区中文字幕在线观看 | 中文字幕第二十六页页 | 久久综合久久受 | 日本久久精品一区 | 欧美电影在线观看网站 | 亚洲精品一区二区三区在线观看 | 狠狠躁夜夜躁人人爽天天高潮 | 日韩在线播 | 天堂免费在线 | 国产精品高潮呻吟久久av野狼 | 国产成人综合在线观看 | 日韩综合区 | 日韩黄色片免费看 | 久久久久久亚洲精品 | 国内精品嫩模av私拍在线观看 | 亚洲欧美激情精品一区二区 | 国产精品一区二区久久久 | 福利在线播放 | 国产在线中文字幕 | 久久精品亚洲精品国产欧美kt∨ | 中文精品在线 | 一区二区免费在线播放 | 色视频在线 | 国产精品一区三区 | 农村末发育av片四区五区 | 激情视频综合网 | 色www精品视频在线观看 | 欧美日韩成人在线 |