0


【security】java springboot项目中使用springSecurity安全框架

第一步,创建springboot的web项目,并导入springSecurity的pom依赖

<!-- spring-security 安全框架依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

<!-- spring-boot的web依赖(如果创建项目时没有勾选,可以手动加上) -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

springboot项目如果导入security依赖后会自动托管整个项目,前端在访问项目的任何路径时会被拦截并跳转到security默认的登录页面,登录用户名为user,密码为控制台启动项目时生成的随机密码

第二步 自定义设置用户的认证和请求权限设置

一、自定义设置用户的认证:创建一个配置类(类上加@Configuration),让该类继承WebSecurityConfigurerAdapter类,并添加@EnableGlobalMethodSecurity( prePostEnabled = true)注解启动权限管理功能,重写configure(AuthenticationManagerBuilder auth)方法实现用户登录认证

1.设置固定配置登录的用户(不常用)

@Configuration
//启动 权限管理功能
@EnableGlobalMethodSecurity( prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    // 重写配置权限的方法
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 固定配置登录的用户
        auth.inMemoryAuthentication() // 内存验证
                .withUser("tom")      //登录的用户名
                .password("{bcrypt}$2a$10$Wg.X1U3cKklWFqiZVmIzSeDynq3LcgXRlVhBIAW0s0tmZLRd.5QWy") 登录的mm
                .authorities("abc")   // 设置权限字符
                .and()
                .withUser("tom2")      //第二个用户,以此类推,.and()后可继续添加用户
                .password("{bcrypt}$2a$10$Wg.X1U3cKklWFqiZVmIzSeDynq3LcgXRlVhBIAW0s0tmZLRd.5QWy") //登录的密码
                .authorities("abc");  // 设置权限字符
    }
}

2.从数据库查询认证,创建一个实现类,通过实现userDetailsService接口,重写里面的loadUserByUsername(String s)方法,当用户点击登录时,此方法的参数会将当前的用户名传入,我们可通过用户名从数据库查询用户信息,将查到的用户信息保存到UserDetails对象中并返回,security框架会将返回的UserDetails对象和当前登录的用户进行认证,如果相同则登录成功。(注意密码必须是带加密的),最后在配置类里设置通过userDetailsServiceI对象验证

package com.security.springSecurity.service;

import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        // 通过用户名去数据库查询密码及权限
        String username = s;
        String password = "{bcrypt}$2a$10$Wg.X1U3cKklWFqiZVmIzSeDynq3LcgXRlVhBIAW0s0tmZLRd.5QWy";//123456
        String authorities = "abc";

        // 将查询到的用户名、密码、权限保存到UserDetails中,框架会自动验证,如果前端传入的和UserDetails中保存一样则会登录成功
        UserDetails userDetails = User
                .builder()
                .username(username)
                .password(password)
                .authorities(authorities)
                .build();
        return userDetails;
    }
}
@Configuration
//启动 权限管理功能
@EnableGlobalMethodSecurity( prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService userDetailsServiceImpl;
    // 重写配置权限的方法
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        // 通过userDetailsServiceImpl对象验证
        auth.userDetailsService(userDetailsServiceImpl);
    }

二、自定义设置请求权限:在配置类里继续重写configure(HttpSecurity http){}方法实现设置请求权限

package com.security.springSecurity.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
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;

@Configuration
//启动 权限管理功能
@EnableGlobalMethodSecurity( prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()        // 对请求进行权限设置
        .antMatchers(                   // 设置匹配路径
                "/index.html",
                "/css/*",
                "/js/*",
                "/login.html",
                "/login"
        ).permitAll()                   // 对以上匹配路径全部放行
        .anyRequest().authenticated()   // 对其他请求需要认证
        .and().formLogin()              // 没有权限会自动跳到登录页
        .loginPage("/login.html")       // 使用自己的登录页面
        .loginProcessingUrl("/login")   // 设置登录时表单提交的路径
        .failureUrl("/login.html?error")// 登录失败的路径
        .defaultSuccessUrl("/index.html")// 设置登录成功的页面
        ;
        http.logout()                   // 设置登出
            .logoutUrl("/logout")       // 设置登出的请求路径
            .logoutSuccessUrl("/login.html");//登出成功后返回的页面

        http.csrf().disable();          //关闭防跨越攻击,最好写上不然容易报错
    }
}

三、使用@PreAuthorize注解给Controller中的请求接口设置访问权限字符:设置后当前用户有该权限字符才能访问该路径,不然会报403权限不足。

四、在任何方法的参数上获取当前登录的用户:通过UserDetails中保存的当前登录用户获取

package com.security.springSecurity.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    //3.在请求路径的方法上加@PreAuthorize("hasAuthority('权限字符')")即可
    @RequestMapping("/s1")
    @PreAuthorize("hasAuthority('abc')")
    public String test01(){
        return "s1";
    }

    //4.在任何方法的参数上加UserDetails参数,并在参数前加@AuthenticationPrincipal注解
    //  此时在使用UserDetails参数就是当前登录时存入的用户
    @RequestMapping("/my")
    public String getMy(@AuthenticationPrincipal UserDetails userDetails){
        String username = userDetails.getUsername();
        return "当前用户用名是:"+username;
    }
}
标签: spring boot java 安全

本文转载自: https://blog.csdn.net/m0_66998390/article/details/126378716
版权归原作者 java的小白兔 所有, 如有侵权,请联系我们删除。

“【security】java springboot项目中使用springSecurity安全框架”的评论:

还没有评论