wiem, że to jest nieco stary, ale miałem tylko do rozwiązania tego samego problemu i nie można było znaleźć coś w nowszych stosów.
Mamy wiele środowisk współdzielących tę samą usługę CAS (pomyśl o środowisku dev, qa, uat i lokalnym środowisku programistycznym); mamy możliwość trafienia w każde środowisko z więcej niż jednego adresu URL (za pośrednictwem serwera WWW po stronie klienta za pośrednictwem odwrotnego proxy i bezpośrednio na serwer zaplecza). Oznacza to, że określenie pojedynczego adresu URL jest w najlepszym wypadku trudne. Być może istnieje sposób, aby to zrobić, ale będąc w stanie używać dynamicznego ServiceProperties.getService()
. Prawdopodobnie dodam jakiś sprawdzian sufiksów serwera, aby upewnić się, że URL nie zostanie w pewnym momencie przejęty.
Oto co zrobiłem, aby uzyskać podstawowe przepływ CAS pracuje niezależnie od adresu URL używanego do uzyskania dostępu do zasobu zabezpieczone ...
- przesłonić
CasAuthenticationFilter
.
- Zastąpienie
CasAuthenticationProvider
.
setAuthenticateAllArtifacts(true)
na ServiceProperties
.
Oto długa forma mojego wiosennego konfiguracji fasoli:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true)
public class CasSecurityConfiguration extends WebSecurityConfigurerAdapter {
Wystarczy zwykła konfiguracja wiosna fasoli.
@Value("${cas.server.url:https://localhost:9443/cas}")
private String casServerUrl;
@Value("${cas.service.validation.uri:/webapi/j_spring_cas_security_check}")
private String casValidationUri;
@Value("${cas.provider.key:whatever_your_key}")
private String casProviderKey;
Niektóre udostępnione parametry konfiguracyjne.
@Bean
public ServiceProperties serviceProperties() {
ServiceProperties serviceProperties = new ServiceProperties();
serviceProperties.setService(casValidationUri);
serviceProperties.setSendRenew(false);
serviceProperties.setAuthenticateAllArtifacts(true);
return serviceProperties;
}
Kluczową rzeczą powyżej jest wywołanie setAuthenticateAllArtifacts(true)
. Spowoduje to, że weryfikator biletów używać realizację AuthenticationDetailsSource
zamiast zakodowanej ServiceProperties.getService()
rozmowy
@Bean
public Cas20ServiceTicketValidator cas20ServiceTicketValidator() {
return new Cas20ServiceTicketValidator(casServerUrl);
}
Standardowy walidatora bilet ..
@Resource
private UserDetailsService userDetailsService;
@Bean
public AuthenticationUserDetailsService authenticationUserDetailsService() {
return new AuthenticationUserDetailsService() {
@Override
public UserDetails loadUserDetails(Authentication token) throws UsernameNotFoundException {
String username = (token.getPrincipal() == null) ? "NONE_PROVIDED" : token.getName();
return userDetailsService.loadUserByUsername(username);
}
};
}
Standardowy hak do istniejącego UserDetailsService
@Bean
public CasAuthenticationProvider casAuthenticationProvider() {
CasAuthenticationProvider casAuthenticationProvider = new CasAuthenticationProvider();
casAuthenticationProvider.setAuthenticationUserDetailsService(authenticationUserDetailsService());
casAuthenticationProvider.setServiceProperties(serviceProperties());
casAuthenticationProvider.setTicketValidator(cas20ServiceTicketValidator());
casAuthenticationProvider.setKey(casProviderKey);
return casAuthenticationProvider;
}
Dostawca standardowego uwierzytelnienia
@Bean
public CasAuthenticationFilter casAuthenticationFilter() throws Exception {
CasAuthenticationFilter casAuthenticationFilter = new CasAuthenticationFilter();
casAuthenticationFilter.setAuthenticationManager(authenticationManager());
casAuthenticationFilter.setServiceProperties(serviceProperties());
casAuthenticationFilter.setAuthenticationDetailsSource(dynamicServiceResolver());
return casAuthenticationFilter;
}
Kluczem tutaj jest dynamicServiceResolver()
ustawienie ..
@Bean
AuthenticationDetailsSource<HttpServletRequest,
ServiceAuthenticationDetails> dynamicServiceResolver() {
return new AuthenticationDetailsSource<HttpServletRequest, ServiceAuthenticationDetails>() {
@Override
public ServiceAuthenticationDetails buildDetails(HttpServletRequest context) {
final String url = makeDynamicUrlFromRequest(serviceProperties());
return new ServiceAuthenticationDetails() {
@Override
public String getServiceUrl() {
return url;
}
};
}
};
}
Dynamicznie tworzy adres URL usługi z metody makeDynamicUrlFromRequest()
. Ten bit jest używany do sprawdzania poprawności biletu.
@Bean
public CasAuthenticationEntryPoint casAuthenticationEntryPoint() {
CasAuthenticationEntryPoint casAuthenticationEntryPoint = new CasAuthenticationEntryPoint() {
@Override
protected String createServiceUrl(final HttpServletRequest request, final HttpServletResponse response) {
return CommonUtils.constructServiceUrl(null, response, makeDynamicUrlFromRequest(serviceProperties())
, null, serviceProperties().getArtifactParameter(), false);
}
};
casAuthenticationEntryPoint.setLoginUrl(casServerUrl + "/login");
casAuthenticationEntryPoint.setServiceProperties(serviceProperties());
return casAuthenticationEntryPoint;
}
Ta część używa tego samego dynamicznego kreatora url, gdy CAS chce przekierować do ekranu logowania.
private String makeDynamicUrlFromRequest(ServiceProperties serviceProperties){
return "https://howeverYouBuildYourOwnDynamicUrl.com";
}
To wszystko, co z niego zrobisz. Przekazałem tylko właściwość ServiceProperties, aby przechowywać identyfikator URI usługi, dla której jesteśmy skonfigurowani. Używamy HATEAOS na tylnej stronie i mieć implementację takiego:
return UriComponentsBuilder.fromHttpUrl(
linkTo(methodOn(ExposedRestResource.class)
.aMethodOnThatResource(null)).withSelfRel().getHref())
.replacePath(serviceProperties.getService())
.build(false)
.toUriString();
EDIT: oto co zrobiłem na liście ważnych przyrostków serwera ..
private List<String> validCasServerHostEndings;
@Value("${cas.valid.server.suffixes:company.com,localhost}")
private void setValidCasServerHostEndings(String endings){
validCasServerHostEndings = new ArrayList<>();
for (String ending : StringUtils.split(endings, ",")) {
if (StringUtils.isNotBlank(ending)){
validCasServerHostEndings.add(StringUtils.trim(ending));
}
}
}
private String makeDynamicUrlFromRequest(ServiceProperties serviceProperties){
UriComponents url = UriComponentsBuilder.fromHttpUrl(
linkTo(methodOn(ExposedRestResource.class)
.aMethodOnThatResource(null)).withSelfRel().getHref())
.replacePath(serviceProperties.getService())
.build(false);
boolean valid = false;
for (String validCasServerHostEnding : validCasServerHostEndings) {
if (url.getHost().endsWith(validCasServerHostEnding)){
valid = true;
break;
}
}
if (!valid){
throw new AccessDeniedException("The server is unable to authenticate the requested url.");
}
return url.toString();
}
Używam sprężyny 3; zwróć uwagę na link do wiosennego zabezpieczenia 3 dokumenty –
Być może [ten link] (https://jira.springsource.org/browse/SEC-1374) jest powiązany i daje wgląd w twoje wymagania/problem? – Raghuram
Cóż, z pewnością nauczyłem się czegoś i wyeliminowałem jedno możliwe rozwiązanie. Ponieważ nie mogę polegać na żądaniu HTTP, nadal chciałbym ustawić usługę za pomocą niektórych wyprowadzonych wartości w czasie wdrażania, który powinien być bezpieczny. –