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

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

SpringBoot Security安裝配置及Thymeleaf整合

瀏覽:2日期:2023-04-05 10:22:28

功能:解決web站點的登錄,權限驗證,授權等功能

優點:在不影響站點業務代碼,可以權限的授權與驗證橫切到業務中

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 下授權與驗證的簡單配置(Security下有登錄,注銷,記住我等功能,可以快速集成到自己的login頁上)Tis:如果template頁中使用了 Frame頁,默認是不能訪問的,需要添加 http.headers().frameOptions().sameOrigin();

@EnableWebSecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter { //授權 @Override protected void configure(HttpSecurity http) throws Exception { //請求授權的規則 http.authorizeRequests()//.antMatchers('/tologin').permitAll() //登錄頁所有人都可以訪問//.antMatchers('/admin/**').hasRole('admin1').antMatchers('/admin/list').hasRole('admin1').antMatchers('/admin/role').hasRole('admin1').antMatchers('/admin/cate').hasRole('admin2').antMatchers('/admin/rule').hasRole('admin2'); // 項目里面使用了springSecurity spring Security下,X-Frame-Options默認為DENY,非spring Security環境下,X-Frame-Options的默認大多也是DENY,這種情況下,瀏覽器拒絕當前頁面加載任何Frame頁面 http.headers().frameOptions().sameOrigin(); //登錄頁(Security默認有一個登錄頁) http.formLogin().permitAll().loginPage('/tologin') //指定自定義的登錄頁地址.successForwardUrl('/admin/index') //登錄成功跳轉地址.usernameParameter('username').passwordParameter('password');//匹配自定義登錄頁的name元素名稱 // 開啟注銷功能,跳轉到登錄頁 http.csrf().disable(); //退出失敗可能能的原因 http.logout().logoutSuccessUrl('/tologin'); //開啟記住我功能,cookie 默認保存14天 http.rememberMe().rememberMeParameter('remember');//匹配自定義登錄頁的name元素名稱 } //認證 @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 頁面整合(添加依賴:thymeleaf-extras-springsecurity)

<!--加入約束--><html xmlns:th='http://www.thymeleaf.org' xmlns:sec='http://www.thymeleaf.org/extras/spring-security'><!-- sec:authorize='isAuthenticated()' 用戶是否登錄 sec:authorize='hasAnyRole(’admin1’)' 是否具有某個角色 sec:authentication='name' 當前登錄用戶 sec:authentication='principal.authorities' 當前用戶全部角色--><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(’權限管理’,’admin/rule’)'> <cite>權限管理</cite> </a></li>

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

標簽: Spring
相關文章:
主站蜘蛛池模板: 国产大乳孕妇喷奶水在线观看 | 欧美成人全部免费观看1314色 | 中文字幕天堂最新版在线网 | 国产三级国产精品国产国在线观看 | 亚洲区精选网址 | 国产亚洲人成网站在线观看 | 99re久久精品国产首页2020 | 日韩三级一区二区 | 欧美日本亚洲国产一区二区 | 一区二区三区国产美女在线播放 | 日本一级毛片片在线播放 | 全部在线播放免费毛片 | 97在线视频网站 | 日本在线观看免费视频 | 国产手机免费视频 | 欧美野外性k8播放性迷宫 | 欧美二级在线观看免费 | 日韩在线国产精品 | 国产日本欧美高清免费区 | 在线观看亚洲欧美 | 99视频在线看观免费 | 男女乱淫真视频免费一级毛片 | 精品国产91久久久久 | 亚洲日本在线看片 | 国产欧美视频在线观看 | 亚洲成人免费网站 | 亚洲免费美女视频 | 2022国产精品手机在线观看 | 九九久久久久久久爱 | 欧美日韩一区二区三区免费 | 欧美日韩国产高清一区二区三区 | 又黄又湿又爽吸乳视频 | 男女上下爽无遮挡午夜免费视频 | 日韩欧美在线视频一区二区 | 波多野结衣视频在线观看地址免费 | 艳女伦交一级毛片 | 久久国产成人午夜aⅴ影院 久久国产成人亚洲精品影院老金 | 国产成人影院在线观看 | 日韩一区二区三区在线观看 | 最新亚洲人成网站在线影院 | 国产亚洲精品成人a在线 |