我对Spring完全陌生,我的任务是创建一个应用程序,通过使用Azure Active对用户进行身份验证来启用SSO。在需要使用接口(供应商提供的接口)时,我有一条严格的规则,该接口将一个方法定义为:
public String authenticate(HttpServletRequest request);此方法将从我们供应商的软件中调用,以便对用户进行身份验证。返回的字符串需要是用户主体名。我遵循了关于如何使用spring安全集成Azure Active的正式文档,并且能够在控制器中检索我的用户信息!我遇到的问题是,当我实现供应商接口时,该方法不再工作,并报告以下错误消息:
出现了一个意外的错误(type=Not Found,status=404)。
我在网上进行了研究,您可以在接口级别上进行注释并在控制器上实现它,但我无法访问供应商接口,该接口仅通过外部JAR文件提供给我。我还看到了一些建议,您可以在其中更新application.properties文件并将以下参数设置为true,但这并没有帮助:
spring.aop.proxy-target-class=true任何帮助都将是美妙的,如果我错过任何重要的细节,因为我是新的生态系统,我道歉。下面是我的代码:
AuthController:
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.security.access.prepost.PreAuthorize;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.generic.CustomerVendorInterface;
@RestController
public class AuthController implements CustomVendorInterface {
@RequestMapping("/")
@PreAuthorize("isAuthenticated()")
@Override
public String authenticate(HttpServletRequest request) {
String username = null;
OAuth2User user = ((OAuth2AuthenticationToken)request.getUserPrincipal()).getPrincipal();
if (user != null)
username = user.getAttribute("upn");
return username;
}
}WebSecurityConfig:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.oauth2.client.oidc.userinfo.OidcUserRequest;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserService;
import org.springframework.security.oauth2.core.oidc.user.OidcUser;
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private OAuth2UserService<OidcUserRequest, OidcUser> oidcUserService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.oauth2Login()
.userInfoEndpoint()
.oidcUserService(oidcUserService);
}
}发布于 2020-09-24 00:51:59
我建议将控制器和身份验证分离为不同的类。下面是如何实现CustomVenderInterface
@Component
public class Authenticator implements CustomVenderInterface {
@Override
public String authenticate(HttpServletRequest request) {
String username = null;
OAuth2User user = ((OAuth2AuthenticationToken)request.getUserPrincipal()).getPrincipal();
if (user != null)
username = user.getAttribute("upn");
return username;
}
}那么控制器类将如下所示:
@RestController
public class AuthController {
private final Authenticator authenticator;
public AuthController(Authenticator authenticator) {
this.authenticator = authenticator;
}
@RequestMapping("/")
@PreAuthorize("isAuthenticated()")
public String handleAuthRequest(HttpServletRequest request) {
return authenticator.authenticate(request);
}
}请展示如何将您的CustomVenderInterface传递到供应商库。
https://stackoverflow.com/questions/64035191
复制相似问题