Security在Spring Boot配置自定义登录接口

第一步:配置SecurityConfiguration文件,Security配置文件

/**
 * @author yu
 * @date 2023/6/14 12:40
 *
 * Security配置类
 */
@Configuration
@EnableWebSecurity
@EnableRedisHttpSession
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = false)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
   

    /**
     * 自定义权限过滤服务,主要用于从数据库对比身份和权限信息
     */
    @Autowired
    private SecurityUserDetailsServiceImpl securityUserDetailsService;

    /**
     * 配置用户认证和授权信息来源
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
   
        auth.userDetailsService(securityUserDetailsService).passwordEncoder(passwordEncoder());
    }

    /**
     * 配置Spring Security的拦截器链
     * @param http http配置
     * @throws Exception 全局异常
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
   
        http
                .cors()
                    .and()
                .authorizeRequests()
                    // 允许访问 Swagger 相关静态资源
                    .antMatchers("/v2/api-docs", "/configuration/**", "/swagger*/**", "/webjars/**").permitAll()
                    // 允许访问登录接口
                    .antMatchers("/v1/login").permitAll()
                    // 需要登录认证后才能访问其他资源
                    .anyRequest().authenticated()
                    .and()
                .<
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
自定义Spring Boot Security的认证过程,您需要实现`UserDetailsService`接口来加载用户信息并验证其凭据。您可以在您的Security配置类中覆盖`configure(AuthenticationManagerBuilder auth)`方法来设置`UserDetailsService`,如下所示: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private MyUserDetailsService userDetailsService; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .antMatchers("/user/**").hasAnyRole("ADMIN", "USER") .antMatchers("/**").permitAll() .and() .formLogin() .and() .logout().logoutSuccessUrl("/login"); } } ``` 在这里,我们使用`MyUserDetailsService`类作为我们的`UserDetailsService`实现。您需要创建此类并实现`loadUserByUsername`方法,该方法将从数据库或其他存储中加载用户信息并返回一个`UserDetails`对象。在这个`UserDetails`对象中,您可以指定用户的密码,角色和权限等详细信息。 ```java @Service public class MyUserDetailsService implements UserDetailsService { @Autowired private UserRepository userRepository; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userRepository.findByUsername(username); if (user == null) { throw new UsernameNotFoundException("User not found with username: " + username); } return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), getAuthorities(user)); } private Collection<? extends GrantedAuthority> getAuthorities(User user) { List<GrantedAuthority> authorities = new ArrayList<>(); for (Role role : user.getRoles()) { authorities.add(new SimpleGrantedAuthority(role.getName())); for (Permission permission : role.getPermissions()) { authorities.add(new SimpleGrantedAuthority(permission.getName())); } } return authorities; } } ``` 在这里,我们使用`UserRepository`类从数据库中加载用户信息。在`loadUserByUsername`方法中,我们从数据库中获取用户信息并返回一个`UserDetails`对象。在`getAuthorities`方法中,我们为用户添加了角色和权限。 这是一个基本的示例,您可以根据您的需求进行更改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值