前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Security 入门(五):在 Spring-Boot中的应用

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

作者头像
程序猿DD
发布2018-03-26 14:45:10
1.5K0
发布2018-03-26 14:45:10
举报
文章被收录于专栏:程序猿DD程序猿DD

前言

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

项目目录结构

Maven 依赖

代码语言:javascript
复制
<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

代码语言:javascript
复制
<!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

代码语言:javascript
复制
<!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

代码语言:javascript
复制
<!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

代码语言:javascript
复制
@SpringBootApplication
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

HomeController.java

代码语言:javascript
复制
@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

代码语言:javascript
复制
@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");
  }
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-01-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序猿DD 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档