SpringSecurity

SpringSecurity框架的使用(安全)

什么是springSecurity?

1、Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架,
它是用于保护基于Spring的应用程序的实际标准。(简称认证与权限)
2、Spring Security是一个框架,致力于为Java应用程序提供身份验证和授权,
与所有Spring项目一样,Spring Security的真正强大之处在于可以轻松扩展以满足自定义要求。

springSecurity的使用就是做到了整个网站开发的安全性,防止漏洞,保护用户隐私,之前我们
所做的拦截器。过滤器,springSecurity都可以帮我们实现。实际上shiro与springSecurity是非常
像的,除了类不一样,功能相似。

核心功能的主要内容

  • 认证(你是谁?识别)
  • 授权 (你能干什么,你所能拥有的权限,比如vip,功能、访问、菜单)
  • 攻击防护(防止伪造身份)

认证(Authentication)授权(Authorization)

项目小实战(springboot项目)

1、需要用到的jar包依赖:
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <version>2.4.2</version>
</dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
</dependency>
2、导入我们的静态资源(源码位置)

link
https://gitee.com/cao_jianhua/spring-security

3、编写Controller层
package com.tao.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@SuppressWarnings("all")
public class RouterController {

    @RequestMapping({"/","/index"})
    public String toIndex(){
        return "index";
    }

    @RequestMapping("/toLogin")
    public String login(){
        return "views/login";
    }

    @RequestMapping("/level1/{id}")
    public String level1(@PathVariable("id")int id){
        return "views/level1/"+id;
    }

    @RequestMapping("/level2/{id}")
    public String level2(@PathVariable("id")int id){
        return "views/level2/"+id;
    }

    @RequestMapping("/level3/{id}")
    public String level3(@PathVariable("id")int id){
        return "views/level3/"+id;
    }
}

这里验证我们所导入的静态资源能否正常显示和访问。

4、开始进入正题,自定义config类,开始springSecurity的配置
package com.tao.Config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //添加用户权限,规定那些角色有权限访问页面
        //首页所有人都可以访问,功能页需要有权限才可以访问
        //请求授权的规则
        //authorize(授权)  authentication(认证)
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/index").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

        //如若没有访问权限则直接跳转到登录页
        // /login
        //这里可以diy我们的login页面
        //由于我们的login页面是定制的,所有要走loginPage请求页面,
        /*由于login.html的form表单请求走的是/login。这个login是springSecurity的,我们对应的也要给后台传送,
        username和password的参数,才能认证该角色的信息*/
        http.formLogin().loginPage("/toLogin").usernameParameter("user").passwordParameter("pwd").loginProcessingUrl("/login");
        //注销 /logout
        http.logout().logoutSuccessUrl("/");
        http.csrf().disable();
        //这里的/login和/logout请求是springSecurity框架中自带的
        //记住我
        http.rememberMe().rememberMeParameter("remember");
    }

    //认证
    //密码编码:PasswordEncoder 是一种安全设置,避免被别人反编译盗取我们的密码
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("xiezhitao").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
                .and()
                .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
    }
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值