【问题标题】:Spring Boot Role Based AuthenticationSpring Boot 基于角色的身份验证
【发布时间】:2017-08-03 08:27:33
【问题描述】:

我对基于 Spring Boot 角色的身份验证有疑问。基本上,我想拥有用户和管理员,并且我想阻止用户访问管理员资源。所以我创建了一个 SecurityConfig 类:

package test;

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.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;

@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

   @Override
   protected void configure(AuthenticationManagerBuilder auth) throws Exception {
       auth.inMemoryAuthentication()
           .withUser("user1").password("password1").roles("USER, ADMIN")
           .and()
           .withUser("user2").password("password2").roles("USER");
   }

   @Override
   protected void configure(HttpSecurity http) throws Exception {
       http.authorizeRequests()
           .antMatchers("/service/test").access("hasRole('USER') or hasRole('ADMIN')")
           .antMatchers("/service/admin").access("hasRole('ADMIN')");
   }
}

这是我的小 REST 服务:

package test;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/service")
public class RestService {

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String echo() {
        return "This is a test";
    }

    @RequestMapping(value = "/admin", method = RequestMethod.GET)
    public String admin() {
        return "admin page";
    }
}

还有我的应用程序类:

package test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableAutoConfiguration 
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

不幸的是,我在执行“curl user1:password1@localhost:8080/service/admin”时总是收到 403“forbidden/access denied”错误消息...我错过了 configure 方法中的任何内容吗?

非常感谢您!

【问题讨论】:

  • 你能做到“user1:password1@localhost:8080/service/test”吗?
  • 不,我似乎收到了所有端点的错误消息。

标签: java spring-boot role


【解决方案1】:

请检查一下。

withUser("user1").password("password1").roles("USER", "ADMIN")

在单独的引号中写入“USER”和“ADMIN”。

【讨论】:

    【解决方案2】:

    我按以下方式更改了它,现在它似乎可以工作了:

    @Configuration
    @EnableWebMvcSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
         @Autowired
         protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
             auth.inMemoryAuthentication()
                .withUser("user1").password("password1").roles("USER", "ADMIN")
                .and()
                .withUser("user2").password("password2").roles("USER");
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.formLogin().permitAll()
                .and()
                .authorizeRequests()
                .antMatchers("/service/test").hasAnyRole("USER", "ADMIN")
                .antMatchers("/service/admin").hasRole("ADMIN")
                .anyRequest().authenticated();
        }
    }
    

    非常感谢您的回答!

    【讨论】:

      【解决方案3】:

      以下设置适用于我的 Spring Boot 应用:

      http.authorizeRequests()
                  .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()//allow CORS option calls
                  .antMatchers("/home", "/").hasAnyAuthority(Role.ROLE_ADMIN, Role.ROLE_USER)
                  .antMatchers("/admin").hasAuthority(Role.ROLE_ADMIN)enter code here
      

      【讨论】:

        猜你喜欢
        • 2021-03-05
        • 2016-01-03
        • 1970-01-01
        • 1970-01-01
        • 2021-06-18
        • 2021-11-22
        • 2019-10-10
        • 2020-07-08
        • 1970-01-01
        相关资源
        最近更新 更多