117.info
人生若只如初见

SpringBoot怎么实现安全认证和授权

Spring Boot提供了Spring Security来实现安全认证和授权功能。下面是一个简单的示例来演示如何在Spring Boot中实现基本的安全认证和授权:

  1. 添加依赖:在pom.xml文件中添加Spring Security的依赖:

    org.springframework.boot
    spring-boot-starter-security

  1. 创建一个WebSecurityConfig类来配置安全规则:
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers("/admin/**").hasRole("ADMIN")
            .antMatchers("/user/**").hasRole("USER")
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .and()
            .httpBasic();
    }
}
  1. 创建一个UserDetailsService实现类来获取用户信息:
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 CustomUserDetailsService implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if ("admin".equals(username)) {
            return User.withDefaultPasswordEncoder().username("admin").password("admin").roles("ADMIN").build();
        } else if ("user".equals(username)) {
            return User.withDefaultPasswordEncoder().username("user").password("user").roles("USER").build();
        } else {
            throw new UsernameNotFoundException("User not found");
        }
    }
}
  1. 创建一个Controller来测试安全认证和授权:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @GetMapping("/admin/test")
    public String adminTest() {
        return "Admin test";
    }

    @GetMapping("/user/test")
    public String userTest() {
        return "User test";
    }
}

这样就可以在Spring Boot中实现基本的安全认证和授权功能了。当访问/admin/test时需要ADMIN角色才能访问,访问/user/test时需要USER角色才能访问。可以通过配置WebSecurityConfig类来定义更复杂的安全规则和用户信息获取方式。

未经允许不得转载 » 本文链接:https://www.117.info/ask/fe249AzsIBgBWAV0.html

推荐文章

  • springboot kafka怎样进行消息自动化测试

    在Spring Boot中使用Kafka进行消息自动化测试,你可以使用Kafka自带的测试工具kafka-console-producer.sh和kafka-console-consumer.sh,或者使用一些专门的测试框...

  • springboot kafka能进行消息流量控制吗

    是的,Spring Boot Kafka可以进行消息流量控制。在Spring Boot中,我们可以使用Kafka的消费者配置参数来实现流量控制。以下是一些建议的配置参数: max.poll.rec...

  • springboot kafka怎样实现消息消费监控

    在Spring Boot中,我们可以使用Kafka的消费者监听器来监控消息消费情况。以下是实现消息消费监控的步骤: 首先,确保你的项目中已经添加了Kafka依赖。在pom.xml文...

  • springboot kafka如何进行消息生产监控

    在Spring Boot应用中使用Kafka进行消息生产监控,可以通过以下几种方式实现: 集成Spring Boot Actuator:
    Spring Boot Actuator提供了很多生产级的功能,包...

  • php中bcsub的功能有哪些

    执行任意精度的减法计算:bcsub函数可以用于在PHP中执行任意精度的减法计算,即使操作数包含小数部分或超过PHP浮点数范围的整数也能够正确计算。 指定小数精度:...

  • Flume怎么实现实时数据的版本控制

    Flume 并不直接支持实时数据的版本控制,它主要用于数据采集、传输和处理。如果需要实现实时数据的版本控制,可以结合使用其他工具或技术,如: 使用 Apache Kaf...

  • SpringMVC中如何实现国际化

    Spring MVC中实现国际化主要有以下几个步骤: 配置MessageSource:在Spring的配置文件中配置一个MessageSource bean,用于加载国际化资源文件。可以使用Resource...

  • SpringMVC中怎么使用AOP

    在Spring MVC中使用AOP需要先定义切面(Aspect),然后将切面织入到需要增强的目标方法中。 创建切面类:编写一个类并使用@Aspect注解标识该类为切面类,同时在类...