Spring Security 入门(五):在 Spring-Boot中的应用
前言
本文作为入门级的DEMO,完全按照官网实例演示;
项目目录结构
Maven 依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<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">
<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>
前端页面 login.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<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> UserName: <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>
前端页面 hello.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<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>
启动程序 Application.java
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
HomeController.java
@Controller
public class HomeController {
@RequestMapping("/")
public String home(){
return "home";
}
@RequestMapping("/login")
public String login(){
return "login";
}
@RequestMapping("/hello")
public String hello(){
return "hello";
}
}
Web安全配置 WebSecurityConfig.java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").permitAll() //请求路径"/"允许访问
.anyRequest().authenticated() //其它请求都需要校验才能访问
.and()
.formLogin()
.loginPage("/login") //定义登录的页面"/login",允许访问
.permitAll()
.and()
.logout() //默认的"/logout", 允许访问
.permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//在内存中注入一个用户名为anyCode密码为password并且身份为USER的对象
auth
.inMemoryAuthentication()
.withUser("anyCode").password("password").roles("USER");
}
}
相关阅读
Spring Security入门(一):登录与退出
Spring Security入门(二):基于数据库验证
Spring Security入门(三):密码加密
Spring Security入门(四):自定义-Filter
推荐阅读
请不要在“微服务”的狂热中迷失自我!
微服务2017年度报告出炉:4大客户画像,15%传统企业已领跑
长按指纹
一键关注
点击 “阅读原文” 看看本号其他精彩内容
Spring Security 入门(五):在 Spring-Boot中的应用相关推荐
- Spring Security入门到实践(一)HTTP Basic在Spring Security中的应用原理浅析
一.Spring Security简介 打开Spring Security的官网,从其首页的预览上就可以看见如下文字: Spring Security is a powerful and highly ...
- Spring Boot基础学习笔记20:Spring Security入门
文章目录 零.学习目标 一.Spring Security (一)Spring Security概述 (二)Spring Boot整合Spring Security实现的安全管理功能 二.基础环境搭建 ...
- Spring Security入门基础
Spring Security入门基础 文章目录 Spring Security入门基础 一,Spring Security的使用 1.1 基本术语 1.2 基本使用 1.2.1 引入依赖 1.2.2 ...
- Spring Security 入门(四):自定义-Filter
前文导读 - Spring Security入门(一):登录与退出 - Spring Security入门(二):基于数据库验证 - Spring Security入门(三):密码加密 本文解决问题 ...
- Spring Security入门(三):密码加密
前文导读 - Spring Security入门(一):登录与退出 - Spring Security入门(二):基于数据库验证 Github 地址 https://github.com/ChinaS ...
- SpringSecurity系列(二) Spring Security入门
1. 新建项目 1.1 新建并启动项目 新建 Spring Boot 项目,添加 Spring Web 和 Spring Security 依赖: 其中 Spring Security 依赖中主要的是 ...
- 【Spring Security入门】06-QQ登录实现
准备工作 1.在 QQ互联 申请成为开发者,并创建应用,得到APP ID 和 APP Key. 2.了解QQ登录时的 网站应用接入流程.(必须看完看懂) 为了方便各位测试,直接把我自己申请的贡献出来: ...
- security框架工作笔记001--- Shiro和Spring Security对比_智慧城市项目中选型用Spring Security
JAVA技术交流QQ群:170933152 Shiro简介 Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Secu ...
- Spring Security入门(一) 导学与开发环境安装
一.导学 课程目标 深入理解Spring Security及相关框架的原理.功能和代码 可以机遇Spring Security及相关框架独立开发认证授权相关功能 掌握抽象和封装的常见技巧,可以编写可重 ...
最新文章
- 取消显示fps的指令_机器人控制指令介绍(一)
- STM32的USB相关资料(转载)
- LeetCode 826. 安排工作以达到最大收益(map)
- Elasticsearch7.15.2 修改IK分词器源码实现基于MySql8的词库热更新
- wordpress主题的样式修改
- 移动端采用Flexible将PX转换REM适配及开发中Retina屏1px边框的两种解决方案
- 9个项目助你在2020年成为前端大师!
- 在Ubuntu和Linux 中安装虚拟机以及安装Windows 10
- 卡巴斯基:揭开“火焰”病毒(Flame)的神秘面纱
- math.js api static function
- 胡乱翻译Apache Ignite(一)
- Activity的概述
- 应届毕业生外包公司体验
- eval函数介绍与用法
- python字体有哪些_python字体推荐 python编程100例
- Hbase篇(7)-Region的分裂
- 无线路由频繁掉线9大原因分析
- GitHub 上值得收藏的100个精选前端项目!你知道几个?
- Pandas学习(3)——Pandas基础
- 100首婚礼背景音乐