Externalize templates and static resources (css, images, favicon, etc.) + Optimization suggestions

102 views
Skip to first unread message

lexiaoyao

unread,
Nov 25, 2021, 8:25:26 AM11/25/21
to CAS Community
1、Create external folder

Create an external folder [/home/fanhl/01-java/cas/cas-templates/src] and put templates, CSS, JS and IMGs here. In this way, you can change the user interface without restarting the service

----------------
└── src
    ├── static
    │   └── wukong
    │       ├── css
    │       ├── favicon.ico
    │       ├── images
    │       ├── js
    │       └── yn.jpg
    ├── templates
    │   └── themes
    │       └── wukong
    │           └── login
    │               └── casLoginView.html
    └── wukong.properties
--------------- 

2、edit cas-overlay project

2.1 Configure service

create resources/services/web-10000001.json
------------
{ "@class" : "org.apereo.cas.services.RegexRegisteredService", "serviceId" : "^(https|imaps|http)://.*", "name" : "web", "id" : 10000001, "evaluationOrder" : 10, "accessStrategy" : { "@class" : "org.apereo.cas.services.DefaultRegisteredServiceAccessStrategy", "enabled" : true, "ssoEnabled" : true }, "theme": "wukong" }
------------

2.2 reference external template, css, imgs

edit application.yml
----------------
cas: service-registry: json: location: classpath:/services watcher-enabled: true authn: accept: users: user::aaa view: template-prefixes: - file:/home/fanhl/01-java/cas/cas-templates/src/templates/ spring: thymeleaf: cache: false web: resources: static-locations: - classpath:/static/ - file:/home/fanhl/01-java/cas/cas-templates/src/static/

----------------

2.3 create wukong.properties

--------
cas.standard.css.file=/css/cas.css
--------
If this file is not configured or empty, the new style cannot be displayed.


3、debug template

3.1、run cas
./gradlew run

3.2 modify casLoginView.html

3.3 look result


4、Optimization suggestions

I think there is a bug here. a mistake in the directory rules of template and theme

CasThymeleafConfiguration.java

theme.setPrefix(viewPath + "themes/%s/"); =>  theme.setPrefix(viewPath + "%s/"); 

 -----------
val templatePrefixes = casProperties.getView().getTemplatePrefixes();
        templatePrefixes.forEach(prefix -> {
            try {
                val prefixPath = ResourceUtils.getFile(prefix).getCanonicalPath();
                val viewPath = StringUtils.appendIfMissing(prefixPath, "/");

                val theme = prefix.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)
                    ? new ThemeClassLoaderTemplateResolver(themeResolver.getObject())
                    : new ThemeFileTemplateResolver(casProperties, themeResolver.getObject());
                configureTemplateViewResolver(theme);
                theme.setPrefix(viewPath + "themes/%s/");
                chain.addResolver(theme);

                val template = prefix.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)
                    ? new ClassLoaderTemplateResolver()
                    : new FileTemplateResolver();
                configureTemplateViewResolver(template);
                template.setPrefix(viewPath);
                chain.addResolver(template);
            } catch (final Exception e) {
                LoggingUtils.warn(LOGGER, String.format("Could not add template prefix '%s' to resolver", prefix), e);
            }
        });

------------


Reply all
Reply to author
Forward
0 new messages