MST

星途 面试题库

面试题:Java Spring Boot中如何进行基于角色的访问控制配置

假设你正在开发一个具有多角色权限的Java Spring Boot应用,如管理员、普通用户。阐述如何配置基于角色的访问控制,确保不同角色能访问特定的资源,同时说明在实际应用中如何通过注解和配置类来实现这一功能,并提及相关的数据库设计思路。
24.5万 热度难度
编程语言Java

知识考点

AI 面试

面试题答案

一键面试

基于角色的访问控制配置

  1. 引入依赖:在pom.xml文件中添加Spring Security依赖,它提供了强大的安全控制功能。
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
  1. 创建角色和用户实体
    • 创建User实体类,包含用户名、密码和角色信息。
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Set;

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String username;
    private String password;
    private Set<Role> roles;

    // 省略getter、setter和构造函数
}
- 创建`Role`实体类,用于表示角色。
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Role {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;

    // 省略getter、setter和构造函数
}
  1. 配置Spring Security
    • 创建一个配置类,继承WebSecurityConfigurerAdapter
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.EnableWebSecurity;
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;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private final UserDetailsService userDetailsService;

    public SecurityConfig(UserDetailsService userDetailsService) {
        this.userDetailsService = userDetailsService;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
           .authorizeRequests()
               .antMatchers("/admin/**").hasRole("ADMIN")
               .antMatchers("/user/**").hasAnyRole("ADMIN", "USER")
               .anyRequest().authenticated()
               .and()
           .formLogin()
               .permitAll()
               .and()
           .logout()
               .permitAll();
    }

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

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

通过注解和配置类实现

  1. 注解方式:在控制器方法上使用@PreAuthorize注解,进行访问权限控制。
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @GetMapping("/dashboard")
    @PreAuthorize("hasRole('ADMIN')")
    public String adminDashboard() {
        return "This is the admin dashboard.";
    }
}
  1. 配置类方式:在上述SecurityConfig类的configure(HttpSecurity http)方法中,通过antMatchers结合hasRolehasAnyRole方法进行配置,决定哪些资源由哪些角色访问。

数据库设计思路

  1. 用户表:存储用户信息,包括用户名、密码(加密存储)等基本信息,外键关联角色表。
CREATE TABLE users (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    role_id BIGINT NOT NULL,
    FOREIGN KEY (role_id) REFERENCES roles(id)
);
  1. 角色表:存储角色信息,如角色名称等。
CREATE TABLE roles (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL UNIQUE
);
  1. 权限表(可选):如果需要更细粒度的权限控制,可以创建权限表,通过中间表关联角色和权限,角色通过权限访问资源。
CREATE TABLE permissions (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL UNIQUE
);

CREATE TABLE role_permissions (
    role_id BIGINT NOT NULL,
    permission_id BIGINT NOT NULL,
    PRIMARY KEY (role_id, permission_id),
    FOREIGN KEY (role_id) REFERENCES roles(id),
    FOREIGN KEY (permission_id) REFERENCES permissions(id)
);