【问题标题】:Mule Authentication via Spring doesn't return 403通过 Spring 进行的 Mule 身份验证不返回 403
【发布时间】:2023-04-07 20:09:01
【问题描述】:

我正在尝试为 Mule 3 上的 Rest 服务创建身份验证。如果我通过一些用户/密码 Spring 拦截它,我可以在我的 java 类上进行身份验证。但是,如果传递了错误的用户/密码并且我返回 null Mule 不会响应 403 错误。我怎样才能让它发挥作用?

骡子xml:

<spring:beans>
    <spring:bean class="br.com.arizona.custom_authentication_provider.CustomAuthenticationProvider" id="customAuthenticationProvider"/>
</spring:beans>

<spring:beans>
    <security:authentication-manager alias="secAuthSample">  
        <security:authentication-provider ref="customAuthenticationProvider"/> 
    </security:authentication-manager>
</spring:beans>

<spring-security:security-manager>
    <spring-security:delegate-security-provider name="memory-provider" delegate-ref="secAuthSample"/>
</spring-security:security-manager>

<flow name="testFlow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/teste" doc:name="HTTP"/>
    <spring-security:http-security-filter  realm="mule-realm" securityProviders="memory-provider" />
    <response>
       <logger message="response" level="INFO" doc:name="Logger"/>
    </response>
    <logger message="passed" level="INFO" doc:name="Logger"/>
</flow>

CustomAuthenticationProvider.java:

package br.com.arizona.custom_authentication_provider;

import java.util.ArrayList;
import java.util.List;

import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String name = authentication.getName();
    String password = authentication.getCredentials().toString();

    System.out.println(name);
    System.out.println(password);

    if (name.equals("admin") && password.equals("system")) {
        List<GrantedAuthority> grantedAuths = new ArrayList<>();
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
        Authentication auth = new UsernamePasswordAuthenticationToken(name, password, grantedAuths);
        return auth;
    } else {
        //throw new BadCredentialsException("Bad Credentials");
        return null;
    }
    }

    @Override
    public boolean supports(Class<?> authentication) {
    return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }
}

【问题讨论】:

    标签: spring authentication spring-security mule


    【解决方案1】:
    【解决方案2】:

    它返回的是什么,你得到 200 吗?

    事情在某种程度上取决于您的流程的其余部分。您可以手动检查身份验证是否正常,并使用消息属性转换器在出站属性范围内将 http.status 设置为 403。这通常是您在使用 REST 时处理其他响应代码的方式。

    我假设您的身份验证机制正在工作,只是在不正确的登录时缺少 403。

    【讨论】:

      猜你喜欢
      • 2018-09-09
      • 2012-06-05
      • 2012-03-06
      • 2013-02-23
      • 2020-05-01
      • 2013-06-12
      • 1970-01-01
      • 2014-07-03
      相关资源
      最近更新 更多