【问题标题】:How to do for authorize endpoints for anonymous user with spring security [closed]如何为具有弹簧安全性的匿名用户授权端点[关闭]
【发布时间】:2019-10-14 09:36:57
【问题描述】:

我正在开发一个带有 Spring Boot 的 SOAP Web 服务和带有基本身份验证的 Spring Security。身份验证有效,但我想授权匿名用户在许多端点访问。我不知道该怎么做。 我想创建 2 个 wsdl,一个用于具有身份验证的端点,另一个用于没有身份验证的端点。可能吗 ? 否则是否可以使用 @PreAuthorize(permitAll) 之类的东西注释端点或自定义 spring 安全性?

什么是正确的做法以及如何做?

提前致谢。

我试过这个:

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "createAuthorRequest")
@ResponsePayload
@PreAuthorize("permitAll()")
public CreateAuthorResponse createAuthor(
        @RequestPayload CreateAuthorRequest request
) throws WSException {
    return authorService.createAuthor(request);
}

或自定义 spring 安全性:

           public void configure(HttpSecurity httpSecurity) throws Exception {

    httpSecurity
    .httpBasic()
            .and()
         .authorizeRequests().antMatchers(HttpMethod.POST, "/ws/createAuthor", "/ws/createAuthorRequest", "/ws/createAuthor**").permitAll()
            .antMatchers(HttpMethod.GET, "/ws/createAuthor", "/ws/createAuthorRequest", "/ws/createAuthor**").permitAll()
            .antMatchers(HttpMethod.PUT, "/ws/createAuthor", "/ws/createAuthorRequest", "/ws/createAuthor**").permitAll()
            .anyRequest().authenticated()

            .and()
            .csrf().disable().headers().frameOptions().disable();

但它不会改变。使用 SOAP,我不知道如何获取被调用端点的名称。这里是spring security的日志:


    2019-05-29 22:49:39.060  INFO 8228 --- [io-8080-exec-10] Spring Security Debugger                 : 

************************************************************

Request received for POST '/ws':

org.apache.catalina.connector.RequestFacade@7445a104

servletPath:/ws
pathInfo:null
headers: 
accept-encoding: gzip,deflate
content-type: text/xml;charset=UTF-8
soapaction: ""
content-length: 516
host: localhost:8080
connection: Keep-Alive
user-agent: Apache-HttpClient/4.1.1 (java 1.5)

【问题讨论】:

    标签: spring-boot soap spring-security


    【解决方案1】:

    是否可以用类似的东西注释端点 @PreAuthorize(permitAll)

    如果您在控制器中使用@PreAuthorize,则只需添加@PreAuthorize("permitAll()")


    或者自定义spring security?

    在您的自定义安全配置中,添加您希望在antMatchers 中公开使用或未经授权的所有端点,然后将其设置为permitAll

    示例:

    @Configuration
    @EnableWebSecurity
    public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.authorizeRequests()
                    // permit all access to these endpoints.
                    .antMatchers("/endpoint1", "/endpoint2", "endpoint3/**").permitAll()
                    // any other request needs to be authenticated
                    .anyRequest().authenticated();
            }
    }
    

    您还可以通过在列表端点之前添加方法作为参数来指定您想要允许的httpmethod

    .antMatchers(HttpMethod.GET, "/endpoint1", "/endpoint2", "endpoint3/**").permitAll()
    .antMatchers(HttpMethod.PUT, "/endpoint1", "endpoint3/**").permitAll()
    .antMatchers(HttpMethod.POST, "endpoint3/**").permitAll()
    

    【讨论】:

    • 感谢您的回答。
    • 我用你的回答更新了帖子
    • 正如您在日志中看到的,您访问了servletPath:/ws,它不包含在您的antmatchers 中。此外,如果我没记错的话,您可以检查您的configuration class 您正在使用的端点的名称。你应该有一个返回ServletRegistrationBeanEndpoint的方法。
    • 我尝试了 2 个文件 wsdl,一个用于端点身份验证,另一个用于其他文件。我改变了我的configuration class,我写了两个方法DefaultWsdl11Definition每个wsdl文件一个,在@Bean方法messageDispatcherServlet中返回ServletRegistrationBean我返回return new ServletRegistrationBean(servlet, "/ws/*","/anonymous/*");。有用。感谢@Royts 的帮助,您的回答对我帮助很大。
    • 恭喜。很高兴能提供帮助。
    猜你喜欢
    • 2015-04-19
    • 2013-07-16
    • 1970-01-01
    • 2015-01-09
    • 2017-01-01
    • 2017-07-21
    • 2017-03-14
    • 2016-01-02
    • 2014-07-21
    相关资源
    最近更新 更多