java - Spring Boot 中基于方法的授权

标签 java spring-security spring-boot

我必须将方法发布为休息服务。 我想在一种方法上应用基本的授权安全性,以免说“gpnfeedback”。 我不想通过 sendgpn 申请任何授权 如何配置SecurityConfig.java?我已使用以下配置,但在调用 http://localhost:7071/gpns/rest/sendgpn 时仍然出现授权错误

Controller

@Controller
@RequestMapping("/gpns/rest/")
public class GpnsRestController {

   @CrossOrigin
   @RequestMapping(value = "/sendgpn", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = { MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE })
   public @ResponseBody
   GpnsResponse sendgpn(@Valid @RequestPart(value = "data", required = true) SendGpnMessageMsisdnListReq sendGpnMessageMsisdnListReq, @Valid @ModelAttribute(value = "photo") MultipartFile photo, @Valid @ModelAttribute(value = "video") MultipartFile video,
         @Valid @ModelAttribute(value = "videothumbnail") MultipartFile videothumbnail) {

   }

   @RequestMapping(method = RequestMethod.POST, value = "/gpnfeedback", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
   public @ResponseBody
   GpnsResponse gpnfeedback(HttpServletRequest http, @Valid @RequestBody GpnFeedbackReq gpnFeedbackReq) {
   }


}

安全性

@Configuration
@EnableWebSecurity(debug = true)
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {


  public static final String ROLE_CLIENT = "CLIENT_USER";

  @Autowired
  private DatabaseAuthenticationProvider databaseAuthenticationProvider;

  @Autowired
  private GpnBasicAuthenticationEntryPoint basicAuthenticationEntryPoint;

   @Override
   public void configure(WebSecurity web) throws Exception {
   web.ignoring().antMatchers("/soap/lb/**");
   }

  @Override
  protected void configure(HttpSecurity http) throws Exception {

    http.csrf().disable();
    http.httpBasic().authenticationEntryPoint(this.basicAuthenticationEntryPoint);
    http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);


    // @formatter:off
    http.authorizeRequests()
      .antMatchers("/gpns/rest/gpnfeedback/**").hasRole(ROLE_CLIENT)           
      .anyRequest().authenticated().and().httpBasic();

    // @formatter:on
  }

  @Override
  protected void configure(AuthenticationManagerBuilder builder) throws Exception {

    //will be invoked in given order

    builder.authenticationProvider(this.databaseAuthenticationProvider);

  }

}

更新1: 我已经更改了以下规则。虽然我可以发送http://localhost:7071/gpns/rest/sendgpn 未经任何授权的方法,http://localhost:7071/gpns/rest/gpnfeedback不被 databaseAuthenticationProvider 处理

http.authorizeRequests()
      .antMatchers("/gpns/rest/gpnfeedback/**").hasRole(ROLE_CLIENT)  
      .antMatchers("/gpns/rest/sendgpn/**").permitAll()          
      .anyRequest().authenticated().and().httpBasic();

最佳答案

我认为您的问题与配置中的这一行有关:

.anyRequest().authenticated().and().httpBasic();

基本上,您在这里所说的是每个请求(除了被忽略的请求)都必须经过身份验证,但您不关心它具有什么角色。尝试使用这个:

.anyRequest().permitAll().and().httpBasic()

或者,如果您只想允许 sendgpn,您可以使用以下命令:

http.authorizeRequests()
      .antMatchers("/gpns/rest/gpnfeedback/**").hasRole(ROLE_CLIENT)  
      .antMatchers("/gpns/rest/sendgpn/**").permitAll()          
      .anyRequest().authenticated().and().httpBasic();

编辑 至于您的更新,我的猜测是您以某种方式错误配置了所提供的内容,或者数据库中的数据不正确。例如,如果 ROLE_CLIENT 的值为“CLIENT”,那么 Spring 会期望 DB 中的值为“ROLE_CLIENT”——它将“ROLE_”前缀添加到角色中。

关于java - Spring Boot 中基于方法的授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38635183/

相关文章:

java - 如何修复图像裁剪的放大尺寸?

java - 其实例为 volatile 的 Singleton 类是否使实例数据再次加载?

java - 如何在android中检查电子邮件格式是否有效

java - http.antMatcher ("/**") .authorizeRequests().antMatchers ("/**") 中的 antMatcher ("/") 需要什么?

spring - 在 Springfox Swagger UI 中如何配置标题、描述和许可证?

java - spring boot中各种数据不匹配如何处理jackson反序列化错误

java - 并行请求的 NullPointer 异常

java - 为什么我要使用 Android 本地服务来执行后台网络任务?

java - Spring Security LDAP 身份验证并从本地数据库收集用户详细信息

java - Spring Security 拦截器 URL 不起作用