Skip to content

Commit

Permalink
thymeleaf整合SpringSecurity5
Browse files Browse the repository at this point in the history
  • Loading branch information
wanglufei committed Apr 11, 2022
1 parent a5e7e37 commit f983d7f
Show file tree
Hide file tree
Showing 15 changed files with 163 additions and 4 deletions.
13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_attoparser_attoparser_2_0_5_RELEASE.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_thymeleaf_thymeleaf_3_0_15_RELEASE.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_unbescape_unbescape_1_1_6_RELEASE.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@
<version>2.6.2</version>
<scope>test</scope>
</dependency>
<!--thymeleaf整合SpringSecurity5依赖-->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<!--Springboot整合thymeleaf依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
7 changes: 7 additions & 0 deletions springsecurity_demo.iml
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,12 @@
<orderEntry type="library" name="Maven: com.beust:jcommander:1.78" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.webjars:jquery:3.5.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test:2.6.2" level="project" />
<orderEntry type="library" name="Maven: org.thymeleaf.extras:thymeleaf-extras-springsecurity5:3.0.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-thymeleaf:2.5.12" level="project" />
<orderEntry type="library" name="Maven: org.thymeleaf:thymeleaf-spring5:3.0.15.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.thymeleaf:thymeleaf:3.0.15.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.attoparser:attoparser:2.0.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.unbescape:unbescape:1.1.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.thymeleaf.extras:thymeleaf-extras-java8time:3.0.4.RELEASE" level="project" />
</component>
</module>
13 changes: 13 additions & 0 deletions src/main/java/com/uin/controller/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* @author wanglufei
Expand Down Expand Up @@ -77,4 +78,16 @@ public String toMain3() {
public String toError() {
return "redirect:error.html";
}

/**
* 跳转页面
*
* @return java.lang.String
* @author wanglufei
* @date 2022/4/11 3:43 PM
*/
@RequestMapping("demo")
public String demo() {
return "demo";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundEx
//2.把查询的密码(注册是已经加过密)进行解析,或者直接把密码放入构造方法
String password = passwordEncoder.encode("123");
return new User(username, password, AuthorityUtils.commaSeparatedStringToAuthorityList(
"admin,normal,ROLE_uin"));
"admin,normal,ROLE_uin,main.html" +
"/insert,/delete"));
}
}
29 changes: 29 additions & 0 deletions src/main/resources/templates/demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5 ">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
登陆账号:<span sec:authentication="name"></span>
登陆账号:<span sec:authentication="principal,username"></span>
凭证:<span sec:authentication="credentials"></span>
权限和角色:<span sec:authentication="authorities"></span>
客户端地址:<span sec:authentication="details,remoteAddress"></span>
sessionId:<span sec:authentication="details.sessionId"></span>

通过权限判断
<button sec:authorize="hasAuthority('/insert')">新增</button>
<button sec:authorize="hasAuthority('/delete')">删除</button>
<button sec:authorize="hasAuthority('/update')">删除</button>
<button sec:authorize="hasAuthority('/select')">查询</button>

通过角色判断
<button sec:authorize="hasRole('uin')">新增</button>
<button sec:authorize="hasRole('uin')">删除</button>
<button sec:authorize="hasRole('uin')">删除</button>
<button sec:authorize="hasRole('uin')">查询</button>
</body>
</html>
4 changes: 3 additions & 1 deletion src/main/resources/templates/error.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<html lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5 ">
<head>
<meta charset="UTF-8">
<title>Title</title>
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/templates/login.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5 ">
<head>
<meta charset="UTF-8">
<title>login</title>
Expand All @@ -8,6 +10,8 @@

<form action="/login" method="post">
用户名:<input type="text" name="username" placeholder="请输入用户名">
用户名:<span type="text" name="username" placeholder="请输入用户名">

密码:<input type="password" name="password" placeholder="请输入密码">
记住我:<input type="checkbox" name="remember-me" value="true">
<button type="submit">登陆</button>
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/templates/main.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<html lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5 ">
<head>
<meta charset="UTF-8">
<title>main</title>
Expand Down

0 comments on commit f983d7f

Please sign in to comment.