• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
漫椿
博客园    首页    新随笔    联系   管理    订阅  订阅
spring Security

  spring Security的作用

1.Spring Security提供了一套Web应用安全性的完整解决方案

  spring scecurity的核心:两个主要核心用户认证(Authentiacation)和用户授权(Authonrization)

1用户认证:验证某个用户是否为系统中的合法主体,也就是说用户能否访问该系统,用户认证一般要求用户提供用户名和密码。系统通过校验完成认证过程。通俗点说就是系统认为用户是否能登入

2.用户授权: 验证某个用户是否有权限执行某个操作,在系统中不同的用户所具有的权限是不同的.

  spring scecurity使用方法

1导入spring scecurity依赖

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

2.在controller中进行测试

@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping("add")
    public  String add(){
        return  "hellospringscurity";
    }

}

3如果访问完路径以后页面是下图这样证明spring security生效;默认的用户名是user,其密码在idea控制台中

 

 

  Spring Security 的基本原理

1  Spring Security本质上就是一个过滤器链里面包含了十几种的过滤器集合

2.FilterSecurityInterceptor、ExceptionTranslationFilter、UsernamePasswordAuthentcationFilter

  基于角色或权限进行访问控制

package com.itjh.securitydemo1.config;

import com.sun.deploy.uitoolkit.impl.awt.AWTPluginUIToolkit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author Y7000
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    UserDetailsService userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(password());

    }

    @Bean
    PasswordEncoder password() {
        return new BCryptPasswordEncoder();

    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //配置没有权限访问的跳转自定义页面
        http.exceptionHandling().accessDeniedPage("/unauth.html");

        //自定义登入页面
        http.formLogin().
                //登入页面设置
                        loginPage("/login.html").
                //登入访问的url
                        loginProcessingUrl("/user/login").
                //登录成功跳转的路径
                        defaultSuccessUrl("/test/index").
                //允许操作
                        permitAll().
                //设置那些路径可以直接访问,不需要认证、
                        and().authorizeRequests().antMatchers("/", "/test/hello", "/user/login").permitAll()
                //当前登入用户只有具有admins权限才可以访问这个路径
                ////第一个方法hasAuthority方法:如果当前的主体具有指定的权限,则返回true反之false
                //.antMatchers("/test/index").hasAuthority("admins")

                //第二个方法hasAnyAuthority方法如果当前主体有任何一个权限则返回true
                //.antMatchers("/test/index").hasAnyAuthority("admins,manager")

                //当前登入用户只有具备ROLE_sale权限才可以访问这个路径
                //第三个方法 hasRole方法 ROLE_sale
                //.antMatchers("/test/index").hasRole("sale")

                //第四个方法 hasAnyRole表示用户具备任何一个条件都可以访问
                .antMatchers("/test/index").hasAnyRole("ROLE_sale,ROLE_admin")

                .anyRequest().authenticated()
                

                //关闭csrf防护
                .and().csrf().disable();


    }

}

 

 

  Spring Security 注解使用

1.@Secured注解使用该注解之前必须在配置类或者启动类中配置 @EnableGlobalMethodSecurity(securedEnabled = true) 注解

@SpringBootApplication
//开启认证授权注解的使用
@EnableGlobalMethodSecurity(securedEnabled = true)
@MapperScan("com.itjh.securitydemo1.mapper")
public class Securitydemo1Application {
    public static void main(String[] args) {
        SpringApplication.run(Securitydemo1Application.class, args);
    }

}

1.1controller方法

@RestController
@RequestMapping("/test")
public class TestController {
    @GetMapping("update")
    //使用该注解之前必须在配置类或者启动类中配置 @EnableGlobalMethodSecurity(securedEnabled = true) 注解
    @Secured({"ROLE_sale","ROLE_manager"})
    public  String update(){
        return  "hello update";
    }
}

2.@PreAuthorize注解controller用于在方法执行之前进行验证

2.1使用注解之前在启动类中配置开启注解使用

@SpringBootApplication
//开启认证授权注解的使用
@EnableGlobalMethodSecurity(prePostEnabled = true)
@MapperScan("com.itjh.securitydemo1.mapper")
public class Securitydemo1Application {
    public static void main(String[] args) {
        SpringApplication.run(Securitydemo1Application.class, args);
    }

}

2.2controller方法

@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping("update")
    //@Secured({"ROLE_sale","ROLE_manager"})
    @PreAuthorize("hasAnyAuthority('admins')")
    public  String update(){

        return  "hello update";
    }


}

 

posted on 2022-05-15 14:03  编程耽误的厨子  阅读(324)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3