SpringSecurity

SpringSecurity

1.介绍

Spring 是非常流行和成功的 Java 应用开发框架,Spring Security 正是 Spring 家族中的成员。Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性的完整解决方案。

2.核心功能及特点

1.核心功能

用户认证(Authentication):系统判断用户是否能登录

用户授权(Authorization):系统判断用户是否有权限去做某些事情

2.特点

  1. 综合性:Spring Security 提供了一套完整的安全解决方案,包括身份验证、授权、会话管理、密码管理等功能。

  2. 可定制性:Spring Security 提供了丰富的配置选项和可扩展的插件机制,可以根据应用程序的需求进行定制。

  3. 安全性:Spring Security 集成了多种安全机制,包括基于角色的访问控制、基于表达式的访问控制、防止 CSRF 攻击、防止会话固定攻击等。

  4. 易用性:Spring Security 提供了简单易用的 API 和配置方式,使开发人员能够快速地集成安全功能到他们的应用程序中。

  5. 社区支持:Spring Security 是由 Spring 社区维护和支持的,拥有庞大的用户群和活跃的开发者社区。

3.入门使用

1.创建Spring Boot项目

加Pom依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

2.运行

直接运行即可,打开浏览器http://localhost:8080/会出现登录页面

账户名默认为user

4.自定义用户名

法一、使用配置文件更改

spring.security.user.name=www            //账号
spring.security.user.password=www        //密码

法二、通过配置类

存放在内存中

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        UserDetails user = User.withDefaultPasswordEncoder()
                .username("test")       //用户名
                .password("123456")   //密码
                .roles("USER")
                .build();
        auth.inMemoryAuthentication()    //存放到内存中
                .withUser(user);
    }

5.自定义登陆页面和权限

登陆页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>


<form action="/userlogin" method="post">
    用户名:<input type="text" name="myname">
    <br/>
    用户名:<input type="password" name="mypwd">
    <br/>
    <input type="submit" value="login">


</form>
</body>
</html>

配置类

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //配置登录from表单
        http.formLogin()
                .loginPage("/login.html")         //登陆页面
                .loginProcessingUrl("/userlogin")
//              .usernameParameter("myname")      //可以重置username参数,html对应即可
//              .passwordParameter("mypwd")      //可以重置username参数,html对应即可
                .defaultSuccessUrl("/success");      //如果通过登陆页面登录成功后重定向到的页面
        http.authorizeRequests().antMatchers("/login.html", "/userlogin", "/").permitAll();        //代表放行
//      http.authorizeRequests().antMatchers("/test").hasRole("USER");
        //test2  有资源    test没有
        http.authorizeRequests().antMatchers("/test").hasAuthority("testp");
        http.authorizeRequests().antMatchers("/test").hasRole("USER");
        http.authorizeRequests().anyRequest().authenticated();      //出去上面放行路径,其他都需要验证
        http.csrf().disable();                                      //关闭csrf    方便HTML文件通过
    }

6.注解的使用

7.获取认证用户信息

1.法一

@GetMapping("user1")
    public Object getUser(Principal principal){
        return principal;
    }

2.法二

@GetMapping("user2")
    public Object getUser2(Authentication principal) {
        return principal;
    }

3.法三

@GetMapping("user3")
    public Object getUser3() {
        Authentication authentication=SecurityContextHolder.getContext().getAuthentication();
        return authentication;
    }

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值