在我们开发项目的过程中经常会用到一些权限管理框架,Java领域里边经常用的可能就是shiro了,与之对应的还有SpringSecurity,SpringSecurity可以说是非常强大,与Spring可以无缝整合,但是学习难度也高,今天给大家分享一个demo级别的。

pom.xml加入以下依赖:

<dependencies>
<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-thymeleaf</artifactId>
</dependency>
</dependencies>

创建一个页面:home.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Example</title>
</head>
<body>
<h1>Welcome!</h1> <p>Click <a th:href="@{/hello}">here</a> to see a greeting.</p>
</body>
</html>

hello.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>
<form th:action="@{/logout}" method="post">
<input type="submit" value="Sign Out"/>
</form>
</body>
</html>

配置MVC端点:

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter { @Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/home").setViewName("home");
registry.addViewController("/").setViewName("home");
registry.addViewController("/hello").setViewName("hello");
registry.addViewController("/login").setViewName("login");
} }

SpringSecurity配置

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { <1>
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
} @Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("admin").password("admin").roles("USER");
}
}

登录页面如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Example </title>
</head>
<body>
<div th:if="${param.error}">
Invalid username and password.
</div>
<div th:if="${param.logout}">
You have been logged out.
</div>
<form th:action="@{/login}" method="post">
<div><label> User Name : <input type="text" name="username"/> </label></div>
<div><label> Password: <input type="password" name="password"/> </label></div>
<div><input type="submit" value="Sign In"/></div>
</form>
</body>
</html>

启动类:

@SpringBootApplication
public class Application { public static void main(String[] args) throws Throwable {
SpringApplication.run(Application.class, args);
} }

有问题可以在下面评论,技术问题可以私聊我。

最新文章

  1. SSRF安全威胁在JAVA代码中的应用
  2. CSS3中:nth-child和:nth-of-type的区别深入理解。 关于:nth-child和:nth-of-type的区别之前一直没太注意,经深入理解才发现里面其实暗藏玄机
  3. FileDataSource java的文件操作
  4. cognos8.3 sample在DB2里的安装
  5. SQL Server 2012数据导入SQL Server 2008
  6. uva 12171 hdu 1771 Sculpture
  7. Java 不使用科学计数法表示数据设置
  8. linux centos7磁盘格式化挂载之parted
  9. pgmpy安装
  10. django xadmin后台页面实现二级联动
  11. bat文件传递参数
  12. HDU 1030(三角数阵 数学)
  13. 解决Windows 系统下Chrome中有多个音频界面时 无法静音单个Tab界面的问题
  14. DJango 基础(6)
  15. 综合评价模型C++实现
  16. Error - SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM
  17. Node.js实战(十)之EventEmitter
  18. 超短reads(primer、barcode、UMI、index等)比对方法
  19. [bug]微信小程序使用 &lt;scroll-view&gt; 和 box-shadow 引起页面抖动
  20. .NET:异常处理的两条“黄金定律”,求批!

热门文章

  1. 服务器Centos7.4 下jdk1.8环境配置、mysql环境搭建,mysql找回(重置)密码看这篇就够了
  2. School Marks-CodeForces
  3. Sound Card Chip
  4. Android 实现形态各异的双向側滑菜单 自己定义控件来袭
  5. webpack-入口篇
  6. echars入门篇
  7. yarn-cli 添加
  8. 工作总结 for 另类写法 循环加时间 集合合并 也是用的 static class Enumerable (IEnumerable&lt;T&gt;的扩展方法) (IEnumerable&lt;T&gt; 的 工具类) (所有集合 数组都实现IEnumerable&lt;T&gt;)
  9. linux c 获取当前执行进程总数
  10. Redis源代码分析(六)--- ziplist压缩列表