SpringBoot-SpringSecurity集成

SpringSecurity的东西还是比较多的,为了尽可能的把项目中用得到的知识都列举出来,近期应该会持续更新SpringBoot-SpringSecurity相关知识

首先我们说一下SpringBoot-SpringSecurity的简单集成
这节我们使用SpringSecurity自带的登陆验证页面

本节代码根据SpringBoot初始化项目 : Maven构建SpringBoot项目 为基础


一,项目目录

先看一下项目目录

目录结构

WebSecurityConfig.java Security配置文件
HelloController.java 设置了一个路由/home 当登陆成功后跳转至此页面


二,为项目pom.xml添加SpringSecurity依赖

<dependencies>

   <!-- 核心模块,包括自动配置支持、日志和YAML -->
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
   </dependency>

   <!-- 测试模块,包括JUnit、Hamcrest、Mockito -->
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
   </dependency>

   <!-- 引入Web模块 -->
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>

   <!-- SpringSecurity依赖 -->
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
   </dependency>

</dependencies>

三,配置SpringSecurity

WebSecurityConfig.java

@Configuration          // 配置文件
@EnableWebSecurity      // 开启Security
@EnableGlobalMethodSecurity(prePostEnabled = true)  //AOP
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        //路由策略和访问权限的简单配置
        http
            .formLogin()                        //启用默认登陆页面
            .failureUrl("/login?error")         //登陆失败返回URL:/login?error
            .defaultSuccessUrl("/home")         //登陆成功跳转URL
            .permitAll();                       //登陆页面全部权限可访问

        super.configure(http);
    }
//
//    /**
//     * 配置内存用户
//     */
//    @Autowired
//    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//        auth
//            .inMemoryAuthentication()
//            .withUser("Brave").password("123").roles("USER");
//    }

}

四,配置路由

helloController.java

/**
 * Created by Brave on 16/11/4.
 */
@RestController
public class helloController {

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public String viewHome() {
        return "hello SpringSecurity";
    }

}

五,测试SpringSecurity

开启服务,访问http://localhost:8080/login
(若直接访问其他路径,会因未登陆鉴权被重定向至http://localhost:8080/login)

login

输入错误的用户名和密码,跳转到 http://localhost:8080/login?error

login-error

输入正确的用户名和密码:

注意:
     使用默认的SpringSecurity配置,当服务启动时会自动生成登陆密码,用户名为user

password

填写正确的用户名和密码后点击登陆,跳转至http://localhost:8080/home

home


当然,我们也可以自己设定用户名,密码,角色(在内存中创建一个用户并授权角色)

WebSecurityConfig.java中打开注释部分重写configureGlobal方法实现
@Configuration          // 配置文件
@EnableWebSecurity      // 开启Security
@EnableGlobalMethodSecurity(prePostEnabled = true)  //AOP
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        //路由策略和访问权限的简单配置
        http
            .formLogin()                        //启用默认登陆页面
            .failureUrl("/login?error")         //登陆失败返回URL:/login?error
            .defaultSuccessUrl("/home")         //登陆成功跳转URL
            .permitAll();                       //登陆页面全部权限可访问

        super.configure(http);
    }

    /**
     * 配置内存用户
     */
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
            .withUser("Brave").password("123").roles("USER");
    }

}

再次开启服务,我们可以使用用户名Brave,密码123进行登陆,账号的角色是ROLE_USER

至此,SpringSecurity集成完成


六,代码下载

  CSDN下载
  
  GitHub下载

维护历史:
20170405:修改了有歧义的描述语句,添加了内存中创建用户的描述

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BraveWangDev

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值