Issue
<bean
name="ticketValidationFilter"
class="org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter"
p:service="https://my.local.service.com/cas-client">
<property name="ticketValidator">
<bean class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">
<constructor-arg index="0" value="https://localhost:8443/cas" />
</bean>
</property>
</bean>
Can anyone please help to convert this xml definition to java config? Specifically, I am confused about how to declare ticketValidator
.
@Bean
FilterRegistrationBean ticketValidationFilter(){
Filter ticketValidationFilter = new
org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter();
FilterRegistrationBean frb = new FilterRegistrationBean();
frb.addUrlPatterns("/*");
frb.setFilter(ticketValidationFilter);
// ticket Validator somewhere here
Map<String, String> initParamMap = new HashMap<String, String>();
initParamMap.put("service", "http://localhost:8889/cas-client");
frb.setInitParameters(initParamMap);
return frb;
}
-----UPDATE FOR FILTERS CONFIG-------
if you use nested beans for filter configuration(Eg. like in my case), whatever properties you set on your filters - they will be null if you use just setters(don't know why, if anyone eager to explain please do).
To solve this issue: create filter(@Component) that extends GenericFilterBean and set your properties there. Then @Autowire it to your config and use setters on filter registration as usual.**
If you have simple configuration property(eg. String) then you can use Map with init params and set it on the FilterRegistration bean.
Solution
Since it's a bean that's used only in this particular external bean, you simply convert it to an instance creation and set the apropriate property.
You can't use Filter
interface as that doesn't have the setter for ticket validator.
Use AbstractTicketValidationFilter
instead:
@Bean
FilterRegistrationBean ticketValidationFilter(){
AbstractTicketValidationFilter ticketValidationFilter = new
org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter();
FilterRegistrationBean frb = new FilterRegistrationBean();
frb.addUrlPatterns("/*");
frb.setFilter(ticketValidationFilter);
//here be ticket validaror
TicketValidator validator = new org.jasig.cas.client.validation.Cas20ServiceTicketValidator("https://localhost:8443/cas");
ticketValidationFilter.setTicketValidator(validator);
//end of ticket validator config
Map<String, String> initParamMap = new HashMap<String, String>();
initParamMap.put("service", "http://localhost:8889/cas-client");
frb.setInitParameters(initParamMap);
return frb;
}
The whole beauty of java config is that some complex xml configurations can be converted to very simple java.
Answered By - soulcheck
Answer Checked By - Robin (JavaFixing Admin)