SpringSecurity权限认证管理

第一阶段:SpringSecurity

  1. 用户认证,授权

用户权限,系统判断用户是否拥有访问资源的权限,自由访问权限的系统资源

  1. 单点登录

    只登录一次,就可以访问同平台的其他系统

  2. 第三方认证,要定义的接口,都会遵循 Oauth2 认证流程

一、快速入门

快速入门项目是以及前后端分离开发为基础的,具体说到自定义拦截接口;没有涉及数据库。

  1. 创建test_secutity Maven项目,并在pom中导入依赖
<properties>
    <java.version>1.8</java.version>
    <spring.security.version>5.1.6.RELEASE</spring.security.version>
    <fastjson.version>1.2.46</fastjson.version>
</properties>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.7.RELEASE</version>
    <relativePath/>
</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-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- spring-security -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${spring.security.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${spring.security.version}</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.25</version>
    </dependency>
    <!-- Mybatis-plus-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <!--JSON-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>${fastjson.version}</version>
    </dependency>
</dependencies>
  1. 创建启动类 SecurityApplication
@SpringBootApplication
@MapperScan("com.security")
public class SecurityApplication {
   
    public static void main(String[] args) {
   
        SpringApplication.run(SecurityApplication.class, args);
    }
}
  1. 创建两个jar包

    • com.security.config 放置Security 配置文件的

    • com.security.controller放置Security 控制类的

  2. 在资源目录下创建application.yml文件,写入以下内容测试一下是否可以正常运行

spring:
  datasource:
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test_security?useUnicode=true&serverTimezone=UTC&useSSL=false
    username: root
    password: 123456
server:
  port: 8082
  1. controller 中创建 TestContrller用于测试接口页面消息,其中getUser数据需要做权限处理,test数据则是测试数据不用做权限
@RestController
public class TestController {
   
    @GetMapping("/getUser")
    public R getUser() {
   
        return R.ok("我是用户数据。。。。。。。。。需要保密!");
    }
    
    @GetMapping("test")
    public R test() {
   
        return R.ok("请求成功了!");
    }
}
  1. config包中 创建WebSecurityConfig
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
   
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
   
        //配置认证方式等
        auth.userDetailsService(userDetailsService());
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
   
        //http相关的配置,包括登入登出、异常处理、会话管理等
        http.cors().and().csrf().disable();
        http.authorizeRequests().
                antMatchers("/getUser").hasAuthority("query_user").and().exceptionHandling();// 配置这个接口需要拦截
    }

}
  1. 测试了,testgetUser接口 可以看出 test接口返回了{"code":0,"data":"请求成功了!","msg":"执行成功"},而getUser接口显示了一个错误界面

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ge80HPLQ-1630852997196)(\images\sa12312a.png)]

  1. 为了让访问没有权限的地址,数据可读,更方便前后端分离开发,我会需要返回JSON数据

    先定义CustomizeAuthenticationEntryPoint

@Component
public class CustomizeAuthenticationEntryPoint implements AuthenticationEntryPoint {
   
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
   
        R result = R.failed("you xx login failed");
   httpServletResponse.setContentType("text/json;charset=utf-8");
        httpServletResponse.getWriter().write(JSON.toJSONString(result));
    }
}

​ 接着在WebSecurityConfig中的configure中配置


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

StarLightLu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值