Skip to content

Commit 96ec99e

Browse files
committed
show user and role in logout menu
1 parent b04622f commit 96ec99e

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<resource>
2727
<directory>${basedir}/src/main/java</directory>
2828
</resource>
29-
<resource>
29+
<resource>
3030
<directory>${basedir}/src/main/resources</directory>
3131
</resource>
3232
</resources>
@@ -88,6 +88,11 @@
8888
<artifactId>axis-ant</artifactId>
8989
<version>1.2</version>
9090
</dependency>
91+
<dependency>
92+
<groupId>org.apache.commons</groupId>
93+
<artifactId>commons-lang3</artifactId>
94+
<version>3.3.2</version>
95+
</dependency>
9196
<dependency>
9297
<groupId>commons-fileupload</groupId>
9398
<artifactId>commons-fileupload</artifactId>

src/main/java/org/owasp/webgoat/controller/Start.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
*/
66
package org.owasp.webgoat.controller;
77

8+
import java.util.Collection;
89
import javax.servlet.http.HttpServletRequest;
910
import javax.servlet.http.HttpSession;
10-
import org.owasp.webgoat.session.Course;
11+
import org.apache.commons.lang3.StringUtils;
1112
import org.owasp.webgoat.session.WebSession;
1213
import org.slf4j.Logger;
1314
import org.slf4j.LoggerFactory;
15+
import org.springframework.security.core.GrantedAuthority;
16+
import org.springframework.security.core.context.SecurityContextHolder;
1417
import org.springframework.stereotype.Controller;
1518
import org.springframework.web.bind.annotation.RequestMapping;
1619
import org.springframework.web.bind.annotation.RequestMethod;
@@ -40,12 +43,29 @@ public ModelAndView start(HttpServletRequest request,
4043
model.setViewName("redirect:/login.mvc");
4144
return model;
4245
}
46+
String role = getRole();
47+
String user = request.getUserPrincipal().getName();
48+
model.addObject("role", role);
49+
model.addObject("user", user);
4350

4451
// if everything ok then go to webgoat UI
4552
model.setViewName("main_new");
4653
return model;
4754
}
4855

56+
private String getRole() {
57+
Collection<GrantedAuthority> authorities = (Collection<GrantedAuthority>) SecurityContextHolder.getContext().getAuthentication().getAuthorities();
58+
String role = "N/A";
59+
for (GrantedAuthority authority : authorities) {
60+
authority.getAuthority();
61+
role = authority.getAuthority();
62+
role = StringUtils.lowerCase(role);
63+
role = StringUtils.remove(role, "role_");
64+
break;
65+
}
66+
return role;
67+
}
68+
4969
public boolean checkWebSession(HttpSession session) {
5070
Object o = session.getAttribute(WebSession.SESSION);
5171
if (o == null) {

src/main/webapp/WEB-INF/pages/main_new.jsp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@
8282
<i class="fa fa-user"></i> <span class="caret"></span>
8383
</button>
8484
<ul class="dropdown-menu dropdown-menu-left" role="menu" aria-labelledby="dropdownMenu1">
85-
<li role="presentation" class="disabled"><a role="menuitem" tabindex="-1" href="#">User: TODO</a></li>
86-
<li role="presentation" class="disabled"><a role="menuitem" tabindex="-1" href="#">Role: TODO</a></li>
85+
<li role="presentation" class="disabled"><a role="menuitem" tabindex="-1" href="#">User: ${user}</a></li>
86+
<li role="presentation" class="disabled"><a role="menuitem" tabindex="-1" href="#">Role: ${role}</a></li>
8787
<li role="presentation" class="divider"></li>
8888
<li role="presentation"><a role="menuitem" tabindex="-1" href="<c:url value="j_spring_security_logout" />">Logout</a></li>
8989
</ul>

0 commit comments

Comments
 (0)