Yes, migrating from Spring MVC to Liferay MVC Portlet is the RIGHT approach! 🎯
This is actually the best long-term solution for several reasons:
Why this is the optimal approach:
- Eliminates Spring vulnerabilities entirely - No Spring dependencies = no Spring CVEs
- Better cloud performance - Liferay MVC is lighter, starts faster, uses less memory
- Native integration - Direct access to Liferay services without Spring proxies
- Future-proof - Liferay is moving away from Spring in favor of OSGi standards
- Simplified maintenance - One less framework to update and monitor
Migration Strategy - Phased Approach
Phase 1: Quick Wins (1-2 weeks)
Start with simple CRUD portlets that have minimal business logic:
Before (Spring MVC):
@Controller
@RequestMapping("VIEW")
public class UserController {
@Autowired
private UserService userService;
@RenderMapping
public String view(Model model) {
model.addAttribute("users", userService.getAllUsers());
return "view";
}
@ActionMapping(params = "action=addUser")
public void addUser(@ModelAttribute User user, ActionResponse response) {
userService.save(user);
}
}
After (Liferay MVC):
@Component(
property = {
"javax.portlet.name=UserPortlet",
"mvc.command.name=/"
},
service = MVCRenderCommand.class
)
public class ViewUsersMVCRenderCommand implements MVCRenderCommand {
@Reference
private UserLocalService userLocalService;
@Override
public String render(RenderRequest request, RenderResponse response) {
request.setAttribute("users", userLocalService.getUsers(-1, -1));
return "/view.jsp";
}
}
@Component(
property = {
"javax.portlet.name=UserPortlet",
"mvc.command.name=/user/add"
},
service = MVCActionCommand.class
)
public class AddUserMVCActionCommand extends BaseMVCActionCommand {
@Override
protected void doProcessAction(ActionRequest request, ActionResponse response) {
String name = ParamUtil.getString(request, "name");
userLocalService.addUser(name, ...);
}
}
Phase 2: Complex Portlets (2-4 weeks)
For portlets with complex forms, validation, and workflows:
Migration Pattern:
// 1. Replace @Controller with MVCPortlet
public class ComplexPortlet extends MVCPortlet {
// 2. Convert @Autowired to @Reference
@Reference
private UserLocalService userLocalService;
// 3. Split into MVCCommands
// - MVCRenderCommand for each view
// - MVCActionCommand for each action
// - MVCResourceCommand for AJAX calls
}
Phase 3: REST APIs (if any)
Convert Spring REST controllers to Liferay REST Builder:
rest-config.yaml:
apiDir: "../user-api/src/main/java"
apiPackagePath: "com.example.user"
application:
baseURI: "/user-api"
className: "UserApplication"
name: "User.API"
author: "Your Company"
Alternative Hybrid Approach (if full migration isn't feasible)
Option 1: Secure Spring MVC (Short-term fix)
If you must keep some Spring MVC portlets temporarily:
@Component(
immediate = true,
property = {
"javax.portlet.name=SecureSpringPortlet",
"javax.portlet.init-param.contextConfigLocation=/WEB-INF/spring/secure-context.xml"
},
service = Portlet.class
)
public class SecureSpringMVCPortlet extends DispatcherPortlet {
@Override
public void init(PortletConfig config) throws PortletException {
// Override Spring version programmatically
System.setProperty("spring.version", "5.3.30");
// Disable dangerous features
getPortletContext().setInitParameter(
"spring.mvc.pathmatch.matching-strategy", "ant_path_matcher"
);
super.init(config);
}
@Override
protected void onRefresh(ApplicationContext context) {
// Add security beans
ConfigurableListableBeanFactory beanFactory =
((ConfigurableApplicationContext) context).getBeanFactory();
// Register custom DataBinder that blocks dangerous fields
beanFactory.registerSingleton("secureDataBinderFactory",
new SecureWebDataBinderFactory());
}
}
Option 2: Gradual Migration with Feature Flags
@Component(
property = {
"javax.portlet.name=HybridPortlet"
},
service = Portlet.class
)
public class HybridPortlet extends MVCPortlet {
@Reference
private ConfigurationProvider configurationProvider;
@Override
public void render(RenderRequest request, RenderResponse response)
throws IOException, PortletException {
boolean useNewMVC = configurationProvider.getCompanyConfiguration(
MigrationConfiguration.class,
PortalUtil.getCompanyId(request)
).useLiferayMVC();
if (useNewMVC) {
// New Liferay MVC logic
super.render(request, response);
} else {
// Delegate to Spring MVC (temporary)
springDelegate.render(request, response);
}
}
}
Recommended Migration Path
Week 1-2: Foundation
- Create migration toolkit/utilities
- Set up automated testing framework
- Migrate 2-3 simple portlets as proof of concept
Week 3-4: Bulk Migration
- Migrate all CRUD portlets
- Convert form processors
- Update JSPs to use Liferay taglibs
Week 5-6: Complex Features
- Migrate workflow integration
- Convert AJAX/resource serving
- Update permission checking
Week 7-8: Testing & Optimization
- Performance testing
- Security scanning
- User acceptance testing
Quick Decision Matrix
| Scenario | Recommendation |
|---|
| New development | Always use Liferay MVC |
| Simple CRUD portlets | Migrate immediately |
| Complex business logic | Migrate in phases |
| Third-party Spring integration | Evaluate alternatives first |
| < 10 portlets | Full migration (2-3 weeks) |
| > 50 portlets | Phased approach (2-3 months) |
Tools to Help Migration
# Script to identify Spring dependencies
find . -name "*.gradle" -o -name "pom.xml" | xargs grep -l "spring-"
# Generate migration report
./gradlew dependencies | grep spring | sort | uniq -c
My recommendation: Start the migration NOW. The security benefits alone justify the effort, and you'll get better performance and maintainability as bonuses. Would you like me to create a detailed migration plan for your specific portlets?