前言

本文作为入门级的DEMO,完全按照官网实例演示;

项目目录结构

Maven 依赖

  1.  <parent>

  2.    <groupId>org.springframework.boot</groupId>

  3.    <artifactId>spring-boot-starter-parent</artifactId>

  4.    <version>1.4.1.RELEASE</version>

  5.  </parent>

  6.  <dependencies>

  7.    <dependency>

  8.      <groupId>org.springframework.boot</groupId>

  9.      <artifactId>spring-boot-starter-web</artifactId>

  10.    </dependency>

  11.    <dependency>

  12.      <groupId>org.springframework.boot</groupId>

  13.      <artifactId>spring-boot-starter-security</artifactId>

  14.    </dependency>

  15.    <dependency>

  16.      <groupId>org.springframework.boot</groupId>

  17.      <artifactId>spring-boot-starter-thymeleaf</artifactId>

  18.    </dependency>

  19.  </dependencies>

前端页面 home.html

  1. <!DOCTYPE html>

  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

  3. <head>

  4.    <title>Spring Security Example</title>

  5. </head>

  6. <body>

  7.  <h1>Welcome!</h1>

  8.  <p>Click <a th:href="@{/hello}">here</a> to see a greeting.</p>

  9. </body>

  10. </html>

前端页面 login.html

  1. <!DOCTYPE html>

  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

  3. <head>

  4.    <title>Spring Security Example </title>

  5. </head>

  6. <body>

  7. <div th:if="${param.error}">    Invalid username and password.</div>

  8. <div th:if="${param.logout}">    You have been logged out.</div>

  9. <form th:action="@{/login}" method="post">    

  10.    <div><label> UserName: <input type="text" name="username"/> </label></div>

  11.    <div><label> Password: <input type="password" name="password"/> </label></div>

  12.    <div><input type="submit" value="Sign In"/></div>

  13. </form>

  14. </body>

  15. </html>

前端页面 hello.html

  1. <!DOCTYPE html>

  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

  3. <head>

  4.    <title>Hello World!</title>

  5. </head>

  6. <body>

  7. <h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>

  8. <form th:action="@{/logout}" method="post">

  9.    <input type="submit" value="Sign Out"/>

  10. </form>

  11. </body>

  12. </html>

启动程序 Application.java

  1. @SpringBootApplication

  2. public class Application {

  3.  public static void main(String[] args) {

  4.    SpringApplication.run(Application.class, args);

  5.  }

  6. }

HomeController.java

  1. @Controller

  2. public class HomeController {

  3.  @RequestMapping("/")

  4.  public String home(){

  5.    return "home";  

  6.  }

  7.  @RequestMapping("/login")

  8.  public String login(){

  9.    return "login";

  10.  }

  11.  @RequestMapping("/hello")

  12.  public String hello(){

  13.    return "hello";

  14.  }

  15. }

Web安全配置 WebSecurityConfig.java

  1. @Configuration

  2. @EnableWebSecurity

  3. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  4.  @Override

  5.  protected void configure(HttpSecurity http) throws Exception {

  6.    http

  7.      .authorizeRequests()

  8.        .antMatchers("/").permitAll()                      //请求路径"/"允许访问

  9.        .anyRequest().authenticated()                      //其它请求都需要校验才能访问

  10.      .and()

  11.        .formLogin()

  12.          .loginPage("/login")                             //定义登录的页面"/login",允许访问

  13.          .permitAll()

  14.      .and()

  15.        .logout()                                           //默认的"/logout", 允许访问

  16.          .permitAll();

  17.  }

  18.  @Autowired

  19.  public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

  20.    //在内存中注入一个用户名为anyCode密码为password并且身份为USER的对象

  21.    auth

  22.      .inMemoryAuthentication()

  23.        .withUser("anyCode").password("password").roles("USER");

  24.  }

  25. }

相关阅读

Spring Security入门(一):登录与退出

Spring Security入门(二):基于数据库验证

Spring Security入门(三):密码加密

Spring Security入门(四):自定义-Filter

推荐阅读

请不要在“微服务”的狂热中迷失自我!

微服务2017年度报告出炉:4大客户画像,15%传统企业已领跑

Dubbo将积极适配Spring Cloud生态

Spring Cloud微服务架构汇总

浅谈微服务基建的逻辑

Service Mesh:下一代微服务

微服务(Microservices)【翻译】

那些没说出口的研发之痛,做与不做微服务的几大理由

长按指纹

一键关注



点击 “阅读原文” 看看本号其他精彩内容

Spring Security 入门(五):在 Spring-Boot中的应用相关推荐

  1. Spring Security入门到实践(一)HTTP Basic在Spring Security中的应用原理浅析

    一.Spring Security简介 打开Spring Security的官网,从其首页的预览上就可以看见如下文字: Spring Security is a powerful and highly ...

  2. Spring Boot基础学习笔记20:Spring Security入门

    文章目录 零.学习目标 一.Spring Security (一)Spring Security概述 (二)Spring Boot整合Spring Security实现的安全管理功能 二.基础环境搭建 ...

  3. Spring Security入门基础

    Spring Security入门基础 文章目录 Spring Security入门基础 一,Spring Security的使用 1.1 基本术语 1.2 基本使用 1.2.1 引入依赖 1.2.2 ...

  4. Spring Security 入门(四):自定义-Filter

    前文导读 - Spring Security入门(一):登录与退出 - Spring Security入门(二):基于数据库验证 - Spring Security入门(三):密码加密 本文解决问题 ...

  5. Spring Security入门(三):密码加密

    前文导读 - Spring Security入门(一):登录与退出 - Spring Security入门(二):基于数据库验证 Github 地址 https://github.com/ChinaS ...

  6. SpringSecurity系列(二) Spring Security入门

    1. 新建项目 1.1 新建并启动项目 新建 Spring Boot 项目,添加 Spring Web 和 Spring Security 依赖: 其中 Spring Security 依赖中主要的是 ...

  7. 【Spring Security入门】06-QQ登录实现

    准备工作 1.在 QQ互联 申请成为开发者,并创建应用,得到APP ID 和 APP Key. 2.了解QQ登录时的 网站应用接入流程.(必须看完看懂) 为了方便各位测试,直接把我自己申请的贡献出来: ...

  8. security框架工作笔记001--- Shiro和Spring Security对比_智慧城市项目中选型用Spring Security

    JAVA技术交流QQ群:170933152 Shiro简介 Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Secu ...

  9. Spring Security入门(一) 导学与开发环境安装

    一.导学 课程目标 深入理解Spring Security及相关框架的原理.功能和代码 可以机遇Spring Security及相关框架独立开发认证授权相关功能 掌握抽象和封装的常见技巧,可以编写可重 ...

最新文章

  1. 取消显示fps的指令_机器人控制指令介绍(一)
  2. STM32的USB相关资料(转载)
  3. LeetCode 826. 安排工作以达到最大收益(map)
  4. Elasticsearch7.15.2 修改IK分词器源码实现基于MySql8的词库热更新
  5. wordpress主题的样式修改
  6. 移动端采用Flexible将PX转换REM适配及开发中Retina屏1px边框的两种解决方案
  7. 9个项目助你在2020年成为前端大师!
  8. 在Ubuntu和Linux 中安装虚拟机以及安装Windows 10
  9. 卡巴斯基:揭开“火焰”病毒(Flame)的神秘面纱
  10. math.js api static function
  11. 胡乱翻译Apache Ignite(一)
  12. Activity的概述
  13. 应届毕业生外包公司体验
  14. eval函数介绍与用法
  15. python字体有哪些_python字体推荐 python编程100例
  16. Hbase篇(7)-Region的分裂
  17. 无线路由频繁掉线9大原因分析
  18. GitHub 上值得收藏的100个精选前端项目!你知道几个?
  19. Pandas学习(3)——Pandas基础
  20. 100首婚礼背景音乐

热门文章

  1. python3 命令行参数处理库 argparse、docopt、click、fire 简介
  2. linux c 运行报错 killed
  3. windows API 创建系统托盘图标
  4. Linux 多线程编程
  5. 安装pyspider后运行pyspider all后遇到的问题
  6. Xen设计的理念--超级调用
  7. C Implement a string class with basic functionality
  8. linux配置文件为yum,Linux系统配置本地yum源
  9. 计算机由那几个基础部分组成,计算机的基本组成由哪些?
  10. 机架搭建_铝型材设备机架定制流程