Cas Resources Link

71 views
Skip to first unread message

Rodrigo Siqueira

unread,
Feb 20, 2019, 9:30:52 AM2/20/19
to CAS Community
Hello guys,

I'm trying to read a list of complementary resources from my application.yml file.
I've tried doing this, which should create repeated "Test" links on login view, but it doesn't works...
Any help is appreciated, thanks



<li class="list-group-item" th:each="resource : ${@environment.getProperty('resources')}">
    <a th:inline="text">
        Test
   
</a>
</li>

resources:
 
- name: name1
   
link: /name1
  -
name: name2
   
link: /name2

Andy Ng

unread,
Feb 20, 2019, 8:41:16 PM2/20/19
to CAS Community
Hi,

I have a sort of similar use case in my CAS 5, I need to use ${@environment.getProperty('mycompany.home.url')} in my casLoginView.html, and my application.yml is as follows:

mycompany.home:

And in my case it works. So I think the use of application.yml and casLoginView.html by you is Ok
===================================================

I think you can try the following:

- Try a simpler test using maybe <p th:text="#{home.welcome}">Welcome to our grocery store!</p> and in application.yml:
home.welcome: some_text_for_debugging_or_something_like_that_lol

- Try see if the location of your application.yml / casLoginView.html is wrong
application.yml should be in "src/main/resources/application.yml", and
casloginView.html should be in "src/main/resources/templates/casLoginView.html"

- It is common mistake to misspell the path name, so double check those. (modified the file to add invalid syntax so see if any error occurs can also check if the pathing is correct)

- Check you CAS version, which you don't specified. I suspect anything CAS version starting from CAS 5 should support the thymeleaf syntax, but at least I use thymelefa starting from CAS 5.1.x and don't have problem like yours

If all the above failed then report back, see if you can give more information so the group and help solve it.

Cheers!
- Andy




Rodrigo Siqueira

unread,
Feb 20, 2019, 8:47:01 PM2/20/19
to CAS Community
Hi Andy,

Yeah, sorry about the lack of info, not used to asking on the web. Anyways, I'm using CAS 5.3.7 if I'm not mistaken...
Your suggestion works as you mentioned, I think it is a start, what I really wished was to use the th:each tag for that <li> so I could load a list of resources dynamically through configuration. 
I'll keep researching... I'll be sure to post here if I find something. 

Thanks man

Andy Ng

unread,
Feb 20, 2019, 9:09:49 PM2/20/19
to CAS Community
Hi,

Oh I see, so your problem is actually for th:each tag....

I see that CAS source code also used th:each in here https://github.com/apereo/cas/blob/v6.0.1/webapp/resources/templates/fragments/loginProviders.html#L26

Maybe that will be helpful to you?

Also don't worry about the lack of info thing, yours are already pretty good in my opinion. If you only said "I can't use thymeleaf please help" than that would be a problem, otherwise is all good.

Cheers!
- Andy

Rodrigo Siqueira

unread,
Feb 20, 2019, 9:15:38 PM2/20/19
to CAS Community
Ooh, nice finding... will try that out first thing in the morning. I hope that works with @environment.getProperty()

If that doesn't work, I'll try creating a bean just for that...

Thanks again!

Andy Ng

unread,
Feb 21, 2019, 3:37:24 AM2/21/19
to CAS Community
Hi there,

Just got some testing done, it seems that either environment.getPreperties() does not support list or is bugged

Because I tested the following (copy from "https://stackoverflow.com/questions/39218966/what-is-null-safe-way-to-convert-array-to-string-using-thymeleaf"):
<span th:text="${@environment.getProperty('resources') != null} ? ${#strings.arrayJoin(@environment.getProperty('resources'), ',')} : 'null value'"></span>

Which should show the list, but instead it outputted null.

So... I think your best bet is to use beans instead... Or maybe someone else have other ideas?

Cheers!
- Andy

Rodrigo Siqueira

unread,
Feb 21, 2019, 9:09:41 AM2/21/19
to CAS Community
Hey, I've got it working now. 
Here's what I've did to get it working:  

Created the following configuration to expose a bean for my properties:
@Configuration
public class MyConfiguration {
   
@Bean
    public MyConfigurationProperties myConfigurationProperties(){
       
return new MyConfigurationProperties();
    }
}

Created a class that represents my properties:
@Getter
@Setter
@ConfigurationProperties(value = "my", ignoreUnknownFields = false)
public class MyConfigurationProperties {

   
@NestedConfigurationProperty
    private MySystemProperties system = new MySystemProperties();

    private List<Resource> resources = new ArrayList<>();

}

@Getter
@Setter
public class MvSystemProperties implements Serializable {
   
private String name;
    private String version;
}


my:
 
system:
   
name: SystemName
   
version: 1.0.0
 
resources:
   
- name: cas.login.resources.res1 #Translatable tag in Thymeleaf using #{${}}
     
url: /res1
    -
name: Sample #If tag does not exists in messageSource it just puts the text as-is (kinda useful)
     
url: /sample


META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  org
.apereo.cas.config.CasEmbeddedContainerTomcatConfiguration,\
  org
.apereo.cas.config.CasEmbeddedContainerTomcatFiltersConfiguration,\
  br
.com.my.sso.properties.MyConfiguration



Thanks again for your help!



Andy Ng

unread,
Feb 21, 2019, 8:34:51 PM2/21/19
to CAS Community
Awesome! A bit frustrated that enviornment.getProperties doesn't support list, but your implementation should be ok. Great work.

Cheers!
- Andy

Ray Bon

unread,
Feb 22, 2019, 11:53:33 AM2/22/19
to cas-...@apereo.org
Rodrigo,

Spring's PropertyResolver, https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/env/PropertyResolver.html, does not provide a list option. It does allow you to specify a type. I am not sure how you would set a list in a text file.
Based on Andy's example, I think you are overriding the properties.
resources:
  name: name1
  link: link1
  name: name2
  link: link2
will become 'resources.name' and 'resources.link'. The '.' or ':' notation ends up being part of the property key. Not like a pseudo class of member variables. The end result is:
resources.link == link2

I do not know much about thyme leaf so this example is pure speculation. Maybe there is a variable processing tag if the below does not work.
Perhaps you could do something with a comma separated list:
resources:
  name1,link1,name2,link2
and then (providing an empty string as a default value):
  th:each="resource : ${split(',', @environment.getProperty('resources', ''))}"

Ray
-- 
Ray Bon
Programmer analyst
Development Services, University Systems
2507218831 | CLE 019 | rb...@uvic.ca

Rodrigo Siqueira

unread,
Feb 22, 2019, 12:23:17 PM2/22/19
to CAS Community
Hey there Ray,

Actually, I'm using:

resources:
    - name:
      link:

Which resolves to resource[0].name/link
In YAML, you can specify lists like that.

Take a look at my previous answer, I was able to load a list through a dedicated property class.
Reply all
Reply to author
Forward
0 new messages