Announcement Announcement Module
Collapse
No announcement yet.
What is the easiest way to access the Authentication object in jstl? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • What is the easiest way to access the Authentication object in jstl?

    I'm currently doing this in a scriptlet:

    <% JanrainAuthenticationToken authentication = (JanrainAuthenticationToken) SecurityContextHolder.getContext().getAuthenticati on();
    UserDataResponse userDataResponse = authentication.getUserDataResponse();
    String plainResponse = userDataResponse.getResponseAsJSONObject().toStrin g(2);
    pageContext.setAttribute("userData", userDataResponse);

    But I'm wondering if can avoid using the scriptlet.

  • #2
    The scriptlet is using custom types and methods to set a page attribute, so I'm not sure how you would expect that to be possible using existing framework functionality, if that's what you mean?

    I think you'd probably have to write your own custom tag.

    Comment


    • #3
      I was using

      <sec:authentication property="principal.userDataResponse.profile.ident ifier" />

      Which works perfectly as it outputs the property as a String.

      However if I want to have a reference to principal.userDataResponse so I was wondering if I could use similar (existing) tag, e.g. in combination with <c:set .. />

      I tried:
      <c:set var="userData">
      <sec:authentication property="principal.userDataResponse" />
      </c:set>

      But then the value in userData is the toString on principal.userDataResponse, and not the actual object.

      Comment


      • #4
        This also works

        <c:set var="authentication" value="<%=(JanrainAuthenticationToken) SecurityContextHolder.getContext().getAuthenticati on() %>" />




        Cheers,
        Marcel

        Comment


        • #5
          If you want to reference the userDataResponse you could do something like:

          Code:
          <sec:authentication var="userData" property="principal.userDataResponse"/>
          <c:out value="${userData}"/>
          <c:out value="${userData.responseAsJSONObject}"/>

          Comment


          • #6
            Thanks Rob,

            <sec:authentication var="userData" property="principal.userDataResponse"/> is exactly what I was looking for... I overlooked the 'var' option in the authentication tag. Sometimes the solution is so simple ;-)


            But to do userDataResponse.getResponseAsJSONObject().toStrin g(2) [*the toString(2)* is important] I think I will have to user scriptlet still.

            Comment


            • #7
              That sounds about right. I'd mistakenly thought that it was the result if the toString method ("plainResponse") that was being set as an attribute whereas it was the previous variable "userDataResponse". So you can do that with the authentication tag as Rob has indicated.

              I think anything that involves passing arguments to a method would need a scriptlet or custom code, though. Not that I'm much of an expert when it comes to JSPs.

              Comment


              • #8
                Originally posted by marceloverdijk View Post
                But to do userDataResponse.getResponseAsJSONObject().toStrin g(2) [*the toString(2)* is important] I think I will have to user scriptlet still.
                I'm not exactly sure what toString(2) method does, but there are some built in functions that may help you. For example the substring method:

                Code:
                <jsp:root   
                   xmlns:jsp="http://java.sun.com/JSP/Page" 
                   xmlns:c="http://java.sun.com/jsp/jstl/core"
                   xmlns:fn="http://java.sun.com/jsp/jstl/functions">
                
                  ....
                
                <c:out value="${fn:substring(userData.responseAsJSONObject, 2, -1)}"/>
                If you are using Spring MVC you can use the eval tag which allows for SpEL to be placed into the expression attribute. Of Course you need to add spring-mvc to your classpath for this approach.

                Code:
                <jsp:root   
                   xmlns:jsp="http://java.sun.com/JSP/Page" 
                   xmlns:spring="http://www.springframework.org/tags">
                <spring:eval expression="userData.responseAsJSONObject.toString(2)"/>
                You can also write your own tags and provide your own EL Functions.

                Comment


                • #9
                  Thanks again Rob,

                  The <spring:eval /> tag does indeed the trick. I wasn't aware of this tag.
                  This should have been part of JSTL!

                  Anyway thanks for help (also Luke), as I really appreciate it.


                  Cheers,
                  Marcel


                  PS: the toString(2) is invoked on org.json.JSONObject and indents the JSON output nicely ;-)

                  Comment


                  • #10
                    I'm glad it worked

                    Originally posted by marceloverdijk View Post
                    PS: the toString(2) is invoked on org.json.JSONObject and indents the JSON output nicely ;-)
                    Thanks for the information. Sorry I am use to jackson and thus wasn't familiar with that method. I was guessing it was removing JSON padding (hence the substring example).

                    Comment

                    Working...
                    X