I previously also tried to execute some JavaScript after user login successful and before redirected back to service, and I successfully created an example.
However, I didn't need to know whether it is Facebook, Google, or Email, unlike your requirement. I just need to execute some JS for all users.
I will give you my example, but you might want to research more on how to determine the Delegate Authentication client (i.e. whether is FB, Google or email).
So, the following is what I changed (CAS 5.3.0)
[It is still a demo testing code, I am not responsible if there is any security / performance issue]:
===================================================================================================================================
1. ExampleCasWebflowContextConfiguration@Configuration("exampleCasWebflowContextConfiguration")
@EnableConfigurationProperties(CasConfigurationProperties.class)
public class ExampleCasWebflowContextConfiguration{
@Autowired
FlowBuilderServices builder;
@Autowired
FlowDefinitionRegistry loginFlowRegistry;
@Autowired
FlowDefinitionRegistry logoutFlowRegistry;
@Autowired
private CasConfigurationProperties casProperties;
@Autowired
private ApplicationContext applicationContext;
@Bean
public CasWebflowConfigurer defaultWebflowConfigurer() {
final ExampleDefaultWebflowConfigurer c = new ExampleDefaultWebflowConfigurer (builder, loginFlowRegistry, applicationContext, casProperties);
c.setLogoutFlowDefinitionRegistry(logoutFlowRegistry);
c.initialize();
return c;
}
@Bean
@RefreshScope
public Action redirectToServiceAction() {
return new ExampleRedirectToServiceAction(responseBuilderLocator);
}
}
===================================================================================================================================
2. ExampleDefaultWebflowConfigurerpublic class ExampleDefaultWebflowConfigurer extends DefaultLoginWebflowConfigurer{
public ExampleDefaultWebflowConfigurer(FlowBuilderServices flowBuilderServices,
FlowDefinitionRegistry flowDefinitionRegistry, ApplicationContext applicationContext,
CasConfigurationProperties casProperties) {
super(flowBuilderServices, flowDefinitionRegistry, applicationContext, casProperties);
}
/**
* Create redirect end state.
*
* @param flow the flow
*/
@Override
protected void createRedirectEndState(final Flow flow) {
createEndState(flow, CasWebflowConstants.STATE_ID_REDIRECT_VIEW, "casGenericRedirectView");
//createEndState(flow, CasWebflowConstants.STATE_ID_REDIRECT_VIEW, "requestScope.url", true);
}
}
===================================================================================================================================
3.
ExampleRedirectToServiceAction
public class ExampleRedirectToServiceAction extends RedirectToServiceAction {
public ExampleRedirectToServiceAction(ResponseBuilderLocator responseBuilderLocator) {
super(responseBuilderLocator);
}
protected Event finalizeResponseEvent(final RequestContext requestContext, final WebApplicationService service,
final Response response) {
String client = ........; //Here you need to research on how you can receive the client (e.g. Google Facebook)
requestContext.getRequestScope().put("client", client);
Event defaultFinalizeResponseEvent = super.finalizeResponseEvent(requestContext, service, response);
return defaultFinalizeResponseEvent;
}
}
===================================================================================================================================
4. src/main/resources/templates/casGenericRedirectView.html<html>
<head>
<script>
/*<![CDATA[*/
var url = /*[[${url}]]*/ null; //This is the url to return to the user after login success
var client = /*[[${client}]]*/ null; //This is your client
//You do your javascript execution here......
/*]]>*/
location.replace(url); // proceed to redirect user to service
// you might also want to redirect user when JavaScript is not enable, you will need to research more on that and put in this file as well
</script>
</head>
===================================================================================================================================
5. src/main/resoruces/META-INF/spring.factoriesorg.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.www.ExampleCasWebflowContextConfiguration
===================================================================================================================================
With this change, you will be able to execute JavaScript after user login success and before they are redirected to their service.
Cheers!
- Andy