【发布时间】: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