最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于spring security實(shí)現(xiàn)登錄注銷(xiāo)功能過(guò)程解析

 更新時(shí)間:2020年01月15日 09:25:48   作者:炫舞風(fēng)中  
這篇文章主要介紹了基于spring security實(shí)現(xiàn)登錄注銷(xiāo)功能過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了基于spring security實(shí)現(xiàn)登錄注銷(xiāo)功能過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

1、引入maven依賴(lài)

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

2、Security 配置類(lèi) 說(shuō)明登錄方式、登錄頁(yè)面、哪個(gè)url需要認(rèn)證、注入登錄失敗/成功過(guò)濾器

@Configuration
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {

  /**
   * 注入 Security 屬性類(lèi)配置
   */
  @Autowired
  private SecurityProperties securityProperties;

  /**
   * 注入 自定義的 登錄成功處理類(lèi)
   */
  @Autowired
  private MyAuthenticationSuccessHandler mySuccessHandler;
  /**
   * 注入 自定義的 登錄失敗處理類(lèi)
   */
  @Autowired
  private MyAuthenticationFailHandler myFailHandler;

  /**
   * 重寫(xiě)PasswordEncoder 接口中的方法,實(shí)例化加密策略
   * @return 返回 BCrypt 加密策略
   */
  @Bean
  public PasswordEncoder passwordEncoder(){
    return new BCryptPasswordEncoder();
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {

    //登錄成功的頁(yè)面地址
    String redirectUrl = securityProperties.getLoginPage();
    //basic 登錄方式
//   http.httpBasic()

    //表單登錄 方式
    http.formLogin()
        .loginPage("/authentication/require")
        //登錄需要經(jīng)過(guò)的url請(qǐng)求
        .loginProcessingUrl("/authentication/form")
        .successHandler(mySuccessHandler)
        .failureHandler(myFailHandler)
        .and()
        //請(qǐng)求授權(quán)
        .authorizeRequests()
        //不需要權(quán)限認(rèn)證的url
        .antMatchers("/authentication/*",redirectUrl).permitAll()
        //任何請(qǐng)求
        .anyRequest()
        //需要身份認(rèn)證
        .authenticated()
        .and()
        //關(guān)閉跨站請(qǐng)求防護(hù)
        .csrf().disable();
    //默認(rèn)注銷(xiāo)地址:/logout
    http.logout().
        //注銷(xiāo)之后 跳轉(zhuǎn)的頁(yè)面
        logoutSuccessUrl("/authentication/require");
  }

3、自定義登錄成功和失敗的處理器

(1)、登錄成功

@Component
@Slf4j
public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
  @Override
  public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {

     logger.info("登錄成功");
     //將 authention 信息打包成json格式返回
      httpServletResponse.setContentType("application/json;charset=UTF-8");
      httpServletResponse.getWriter().write("登錄成功");
 } }

(2)、登錄失敗

@Component
@Slf4j
public class MyAuthenticationFailHandler extends SimpleUrlAuthenticationFailureHandler {
  @Override
  public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
    logger.info("登錄失敗");

      //設(shè)置狀態(tài)碼
      httpServletResponse.setStatus(500);
      //將 登錄失敗 信息打包成json格式返回
      httpServletResponse.setContentType("application/json;charset=UTF-8");
      httpServletResponse.getWriter().write("登錄失?。?+e.getMessage());
 } }

4、UserDetail 類(lèi) 加載用戶(hù)數(shù)據(jù) , 返回UserDetail 實(shí)例 (里面包含用戶(hù)信息)

@Component
@Slf4j
public class MyUserDetailsService implements UserDetailsService {

  @Autowired
  private PasswordEncoder passwordEncoder;

  /**
   * 根據(jù)進(jìn)行登錄
   * @param username
   * @return
   * @throws UsernameNotFoundException
   */
  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    log.info("登錄用戶(hù)名:"+username);
    String password = passwordEncoder.encode("123456");
    //User三個(gè)參數(shù)  (用戶(hù)名+密碼+權(quán)限)
    //根據(jù)查找到的用戶(hù)信息判斷用戶(hù)是否被凍結(jié)
    log.info("數(shù)據(jù)庫(kù)密碼:"+password);
    return new User(username,password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
  }
}

5、登錄路徑請(qǐng)求類(lèi),.loginPage("/authentication/require")

@RestController
@Slf4j
@ResponseStatus(code = HttpStatus.UNAUTHORIZED)
public class BrowerSecurityController {

  /**
   * 把當(dāng)前的請(qǐng)求緩存到 session 里去
   */
  private RequestCache requestCache = new HttpSessionRequestCache();

  /**
   * 重定向 策略
   */
  private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

  /**
   * 注入 Security 屬性類(lèi)配置
   */
  @Autowired
  private SecurityProperties securityProperties;

  /**
   * 當(dāng)需要身份認(rèn)證時(shí) 跳轉(zhuǎn)到這里
   */
  @RequestMapping("/authentication/require")
  public SimpleResponse requireAuthentication(HttpServletRequest request, HttpServletResponse response) throws IOException {
    //拿到請(qǐng)求對(duì)象
    SavedRequest savedRequest = requestCache.getRequest(request, response);
    if (savedRequest != null){
      //獲取 跳轉(zhuǎn)url
      String targetUrl = savedRequest.getRedirectUrl();
      log.info("引發(fā)跳轉(zhuǎn)的請(qǐng)求是:"+targetUrl);

      //判斷 targetUrl 是不是 .html 結(jié)尾, 如果是:跳轉(zhuǎn)到登錄頁(yè)(返回view)
      if (StringUtils.endsWithIgnoreCase(targetUrl,".html")){
        String redirectUrl = securityProperties.getLoginPage();
        redirectStrategy.sendRedirect(request,response,redirectUrl);
      }
    }
    //如果不是,返回一個(gè)json 字符串
    return new SimpleResponse("訪問(wèn)的服務(wù)需要身份認(rèn)證,請(qǐng)引導(dǎo)用戶(hù)到登錄頁(yè)");
  }

6、postman請(qǐng)求測(cè)試

(1)未登錄請(qǐng)求

(2)、登錄

(3)、再次訪問(wèn)

(4)、注銷(xiāo)

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

相關(guān)文章

  • Hibernate延遲加載原理與實(shí)現(xiàn)方法

    Hibernate延遲加載原理與實(shí)現(xiàn)方法

    這篇文章主要介紹了Hibernate延遲加載原理與實(shí)現(xiàn)方法,較為詳細(xì)的分析了Hibernate延遲加載的概念,原理與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2016-03-03
  • Java使用Calendar類(lèi)實(shí)現(xiàn)動(dòng)態(tài)日歷

    Java使用Calendar類(lèi)實(shí)現(xiàn)動(dòng)態(tài)日歷

    這篇文章主要為大家詳細(xì)介紹了Java使用Calendar類(lèi)實(shí)現(xiàn)動(dòng)態(tài)日歷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • JavaWeb開(kāi)發(fā)中alias攔截器的使用方法

    JavaWeb開(kāi)發(fā)中alias攔截器的使用方法

    本文給大家介紹在JavaWeb開(kāi)發(fā)中alias攔截器的使用方法相關(guān)知識(shí),本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起看下吧
    2016-08-08
  • 解決springboot 2.x 里面訪問(wèn)靜態(tài)資源的坑

    解決springboot 2.x 里面訪問(wèn)靜態(tài)資源的坑

    這篇文章主要介紹了解決springboot 2.x 里面訪問(wèn)靜態(tài)資源的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java for循環(huán)幾種寫(xiě)法整理

    Java for循環(huán)幾種寫(xiě)法整理

    這篇文章主要介紹了Java for循環(huán)幾種寫(xiě)法整理的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Mybatis Properties 配置優(yōu)先級(jí)詳解

    Mybatis Properties 配置優(yōu)先級(jí)詳解

    這篇文章主要介紹了Mybatis Properties 配置優(yōu)先級(jí),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringBoot如何統(tǒng)一配置bean的別名

    SpringBoot如何統(tǒng)一配置bean的別名

    這篇文章主要介紹了SpringBoot如何統(tǒng)一配置bean的別名,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Java?獲取Zookeeper節(jié)點(diǎn)下所有數(shù)據(jù)詳細(xì)步驟

    Java?獲取Zookeeper節(jié)點(diǎn)下所有數(shù)據(jù)詳細(xì)步驟

    本文介紹了如何使用Java獲取ZooKeeper節(jié)點(diǎn)下所有數(shù)據(jù),實(shí)際應(yīng)用示例中,我們演示了如何從ZooKeeper節(jié)點(diǎn)下獲取配置信息并輸出到控制臺(tái),ZooKeeper是一個(gè)開(kāi)源的分布式協(xié)調(diào)服務(wù),適用于分布式系統(tǒng)中的數(shù)據(jù)同步、配置管理、命名服務(wù)等功能,感興趣的朋友一起看看吧
    2024-11-11
  • springboot跨域過(guò)濾器fetch react Response to preflight request doesn‘t pass access control check問(wèn)題

    springboot跨域過(guò)濾器fetch react Response to p

    這篇文章主要介紹了springboot跨域過(guò)濾器fetch react Response to preflight request doesn‘t pass access control check問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Java中常用文本加解密工具類(lèi)總結(jié)

    Java中常用文本加解密工具類(lèi)總結(jié)

    這篇文章主要為大家詳細(xì)介紹了Java中常用的幾種文本加解密工具類(lèi),包括AES對(duì)稱(chēng)加密、RSA非對(duì)稱(chēng)加密、哈希算法加密和Base64編解碼,需要的可以參考下
    2024-11-11

最新評(píng)論

聂荣县| 阿荣旗| 静乐县| 高陵县| 安福县| 宜黄县| 阿城市| 章丘市| 辛集市| 改则县| 金阳县| 安徽省| 黔南| 图木舒克市| 思茅市| 阳西县| 吉木乃县| 淳化县| 报价| 南通市| 凤阳县| 天柱县| 绥化市| 徐闻县| 永仁县| 台中市| 扎兰屯市| 湖州市| 新昌县| 邯郸市| 平谷区| 阿瓦提县| 岢岚县| 汉源县| 栾川县| 黎城县| 台北县| 甘南县| 江孜县| 六安市| 邻水|