亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot Security安裝配置及Thymeleaf整合

 更新時(shí)間:2020年12月04日 08:36:11   作者:人間有妖氣  
這篇文章主要介紹了SpringBoot Security安裝配置及Thymeleaf整合,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

功能:解決web站點(diǎn)的登錄,權(quán)限驗(yàn)證,授權(quán)等功能

優(yōu)點(diǎn):在不影響站點(diǎn)業(yè)務(wù)代碼,可以權(quán)限的授權(quán)與驗(yàn)證橫切到業(yè)務(wù)中

1、要添加的依賴

<!--thymeleaf-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <!--security 和 thymeleaf 整合包-->
    <dependency>
      <groupId>org.thymeleaf.extras</groupId>
      <artifactId>thymeleaf-extras-springsecurity5</artifactId>
    </dependency>
    <!--web-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--security-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

2、Security 下授權(quán)與驗(yàn)證的簡(jiǎn)單配置(Security下有登錄,注銷,記住我等功能,可以快速集成到自己的login頁(yè)上)
Tis:如果template頁(yè)中使用了 Frame頁(yè),默認(rèn)是不能訪問的,需要添加 http.headers().frameOptions().sameOrigin();

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  //授權(quán)
  @Override
  protected void configure(HttpSecurity http) throws Exception {

    //請(qǐng)求授權(quán)的規(guī)則
    http.authorizeRequests()
        //.antMatchers("/tologin").permitAll() //登錄頁(yè)所有人都可以訪問
        //.antMatchers("/admin/**").hasRole("admin1")
        .antMatchers("/admin/list").hasRole("admin1")
        .antMatchers("/admin/role").hasRole("admin1")
        .antMatchers("/admin/cate").hasRole("admin2")
        .antMatchers("/admin/rule").hasRole("admin2");

    // 項(xiàng)目里面使用了springSecurity spring Security下,X-Frame-Options默認(rèn)為DENY,非spring Security環(huán)境下,X-Frame-Options的默認(rèn)大多也是DENY,這種情況下,瀏覽器拒絕當(dāng)前頁(yè)面加載任何Frame頁(yè)面
    http.headers().frameOptions().sameOrigin();

    //登錄頁(yè)(Security默認(rèn)有一個(gè)登錄頁(yè))
    http.formLogin().permitAll()
        .loginPage("/tologin") //指定自定義的登錄頁(yè)地址
        .successForwardUrl("/admin/index") //登錄成功跳轉(zhuǎn)地址
        .usernameParameter("username").passwordParameter("password");//匹配自定義登錄頁(yè)的name元素名稱

    // 開啟注銷功能,跳轉(zhuǎn)到登錄頁(yè)
    http.csrf().disable(); //退出失敗可能能的原因
    http.logout().logoutSuccessUrl("/tologin");

    //開啟記住我功能,cookie 默認(rèn)保存14天
    http.rememberMe()
        .rememberMeParameter("remember");//匹配自定義登錄頁(yè)的name元素名稱

  }

  //認(rèn)證
  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
        .passwordEncoder(new BCryptPasswordEncoder())//密碼加密方式(有些版本的Security必須要指定)
        .withUser("root").password(new BCryptPasswordEncoder().encode("123")).roles("admin1","admin2","admin3")
        .and()
        .withUser("yeqiu").password(new BCryptPasswordEncoder().encode("123")).roles("admin1")
        .and()
        .withUser("admin").password(new BCryptPasswordEncoder().encode("123")).roles("admin2");

  }
}

3、Security 和 Thymeleaf 頁(yè)面整合(添加依賴:thymeleaf-extras-springsecurity)

<!--加入約束-->
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

<!--
  sec:authorize="isAuthenticated()" 用戶是否登錄
  sec:authorize="hasAnyRole('admin1')" 是否具有某個(gè)角色
  sec:authentication="name" 當(dāng)前登錄用戶
  sec:authentication="principal.authorities" 當(dāng)前用戶全部角色
-->

<div sec:authorize="isAuthenticated()">
  <h2><span sec:authentication="name"></span>,您好 您的身份是
    <span sec:authentication="principal.authorities"></span>
  </h2>
</div>

<li sec:authorize="hasRole('admin2')">
   <a onclick="xadmin.add_tab('權(quán)限管理','admin/rule')">
      <cite>權(quán)限管理</cite>
   </a>
</li>

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

相關(guān)文章

  • Java 5個(gè)人坐在一起(有關(guān)第五個(gè)人歲數(shù)的問題)

    Java 5個(gè)人坐在一起(有關(guān)第五個(gè)人歲數(shù)的問題)

    利用遞歸的方法,遞歸分為回推和遞推兩個(gè)階段。要想知道第五個(gè)人歲數(shù),需知道第四人的歲數(shù),依次類推,推到第一人(10歲),再往回推,需要的朋友可以參考下
    2017-02-02
  • 利用Postman和Chrome的開發(fā)者功能探究項(xiàng)目(畢業(yè)設(shè)計(jì)項(xiàng)目)

    利用Postman和Chrome的開發(fā)者功能探究項(xiàng)目(畢業(yè)設(shè)計(jì)項(xiàng)目)

    這篇文章主要介紹了利用Postman和Chrome的開發(fā)者功能探究項(xiàng)目(畢業(yè)設(shè)計(jì)項(xiàng)目),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • springboot整合Mybatis-plus的實(shí)現(xiàn)

    springboot整合Mybatis-plus的實(shí)現(xiàn)

    這篇文章主要介紹了springboot整合Mybatis-plus的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Spring中Bean的作用域與生命周期詳解

    Spring中Bean的作用域與生命周期詳解

    這篇文章主要給大家介紹了Spring中Bean的生命周期和作用域及實(shí)現(xiàn)方式的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧
    2021-08-08
  • 關(guān)于springboot使用rocketmq?RocketMQMessageListener參數(shù)問題

    關(guān)于springboot使用rocketmq?RocketMQMessageListener參數(shù)問題

    這篇文章主要介紹了springboot使用rocketmq?RocketMQMessageListener參數(shù)問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值需要的朋友可以參考下
    2022-11-11
  • Proxy實(shí)現(xiàn)AOP切面編程案例

    Proxy實(shí)現(xiàn)AOP切面編程案例

    這篇文章主要介紹了Proxy實(shí)現(xiàn)AOP切面編程案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2020-08-08
  • 使用Springboot自定義轉(zhuǎn)換器實(shí)現(xiàn)參數(shù)去空格功能

    使用Springboot自定義轉(zhuǎn)換器實(shí)現(xiàn)參數(shù)去空格功能

    這篇文章主要介紹了使用Springboot自定義轉(zhuǎn)換器實(shí)現(xiàn)參數(shù)去空格功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java-String類最全匯總(上篇)

    Java-String類最全匯總(上篇)

    這篇文章主要介紹了Java-String類最全匯總(上篇),本文章內(nèi)容詳細(xì),本模塊分為了兩部分,本次為上篇,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2023-01-01
  • springboot?html調(diào)用js無(wú)效400問題及解決

    springboot?html調(diào)用js無(wú)效400問題及解決

    這篇文章主要介紹了springboot?html調(diào)用js無(wú)效400的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 使用Spring的ApplicationEvent實(shí)現(xiàn)本地事件驅(qū)動(dòng)的實(shí)現(xiàn)方法

    使用Spring的ApplicationEvent實(shí)現(xiàn)本地事件驅(qū)動(dòng)的實(shí)現(xiàn)方法

    本文介紹了如何使用Spring的ApplicationEvent實(shí)現(xiàn)本地事件驅(qū)動(dòng),通過自定義事件和監(jiān)聽器,實(shí)現(xiàn)模塊之間的松耦合,提升代碼的可維護(hù)性和擴(kuò)展性。同時(shí)還介紹了異步事件和事件傳遞的相關(guān)知識(shí)
    2023-04-04

最新評(píng)論