Spring Security 入门(五):在 Spring-Boot中的应用

前言

本文作为入门级的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)【翻译】

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


长按指纹

一键关注


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


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值