這篇文章主要講解了“spring security的簡單例子分析”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“spring security的簡單例子分析”吧!
遂溪ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!
1 pom.的主要文件 我引入的thymeleaf-extras-springsecurity5,springboot2.1.6 <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 --> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> 2 controller [@Controller](https://my.oschina.net/u/1774615)
public class KungfuController {
private final String PREFIX = "pages/"; @RequestMapping("/") public String index() { System.out.println("hello word"); return "welcome"; } @RequestMapping("/userlogin") public String loginPage() { return PREFIX+"login1"; } @GetMapping("/level1/{path}") public String level1(@PathVariable("path")String path) { return PREFIX+"level1/"+path; } @GetMapping("/level2/{path}") public String level2(@PathVariable("path")String path) { return PREFIX+"level2/"+path; } @GetMapping("/level3/{path}") public String level3(@PathVariable("path")String path) { return PREFIX+"level3/"+path; }
}
//配置下
@EnableWebSecurity public class mySecurity extends WebSecurityConfigurerAdapter {
//為啥引入這個bean ,因為在securety在5.0后使用系統(tǒng)的登錄模板,默認把密碼給加密啦,這個寫的是不讓密碼加密
@Bean public static NoOpPasswordEncoder passwordEncoder() { return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance(); } protected void configure(HttpSecurity http) throws Exception { //定制請求的授權(quán)規(guī)則 http.authorizeRequests().antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("VIP1") .antMatchers("/level2/**").hasRole("VIP2") .antMatchers("/level3/**").hasRole("VIP3"); //開啟自動登錄的功能 http.formLogin(); //開啟自動配置的注銷功能 http.logout().logoutSuccessUrl("/"); } public void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().withUser("mao").password("123").roles("VIP1","VIP2") .and().withUser("zhang").password("123").roles("VIP1","VIP3").and() .withUser("li").password("123").roles("VIP2","VIP3"); }
} //該模板都是尚學堂的
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2 align="center">歡迎光臨武林秘籍管理系統(tǒng)</h2>
<div sec:authorize="!isAuthenticated()">
<h3 align="center">游客您好,如果想查看武林秘籍 <a th:href="@{/login}">請登錄</a></h3>
</div>
<div sec:authorize="isAuthenticated()">
<h3><span sec:authentication="name"></span>,您好,您的角色有: <span sec:authentication="principal.authorities"></span></h3> <form th:action="@{/logout}" method="post"> <input type="submit" value="注銷"/> </form>
感謝各位的閱讀,以上就是“spring security的簡單例子分析”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對spring security的簡單例子分析這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
網(wǎng)站名稱:springsecurity的簡單例子分析
當前路徑:http://jinyejixie.com/article30/ijjhso.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站內(nèi)鏈、外貿(mào)網(wǎng)站建設、靜態(tài)網(wǎng)站、做網(wǎng)站、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)