Reorganizing package directories
Project: http://git-wip-us.apache.org/repos/asf/struts-examples/repo Commit: http://git-wip-us.apache.org/repos/asf/struts-examples/commit/ee5a3e3a Tree: http://git-wip-us.apache.org/repos/asf/struts-examples/tree/ee5a3e3a Diff: http://git-wip-us.apache.org/repos/asf/struts-examples/diff/ee5a3e3a Branch: refs/heads/master Commit: ee5a3e3abe8f6cb56714789958573a882e83aa7b Parents: a6ad31d Author: tkofford <tkoff...@home.ku.edu> Authored: Thu Jul 6 10:31:57 2017 -0500 Committer: tkofford <tkoff...@home.ku.edu> Committed: Thu Jul 6 10:31:57 2017 -0500 ---------------------------------------------------------------------- .../shiro/example/action/LoginAction.java | 116 +++++++++++++++++++ .../shiro/example/action/LogoutAction.java | 18 +++ .../shiro/example/action/ShiroBaseAction.java | 28 +++++ .../shiro/example/action/WelcomeAction.java | 107 +++++++++++++++++ .../interceptor/ShiroUserInterceptor.java | 57 +++++++++ .../struts2shiro/action/LoginAction.java | 113 ------------------ .../struts2shiro/action/LogoutAction.java | 18 --- .../struts2shiro/action/ShiroBaseAction.java | 28 ----- .../struts2shiro/action/WelcomeAction.java | 101 ---------------- .../interceptor/ShiroUserInterceptor.java | 57 --------- 10 files changed, 326 insertions(+), 317 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts-examples/blob/ee5a3e3a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/action/LoginAction.java ---------------------------------------------------------------------- diff --git a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/action/LoginAction.java b/shiro-basic/src/main/java/org/apache/struts2/shiro/example/action/LoginAction.java new file mode 100644 index 0000000..88f7ba5 --- /dev/null +++ b/shiro-basic/src/main/java/org/apache/struts2/shiro/example/action/LoginAction.java @@ -0,0 +1,116 @@ +package org.apache.struts2.shiro.example.action; + +import org.apache.shiro.SecurityUtils; +import org.apache.shiro.authc.AuthenticationException; +import org.apache.shiro.authc.IncorrectCredentialsException; +import org.apache.shiro.authc.LockedAccountException; +import org.apache.shiro.authc.UnknownAccountException; +import org.apache.shiro.authc.UsernamePasswordToken; +import org.apache.shiro.session.Session; +import org.apache.shiro.subject.Subject; +import com.opensymphony.xwork2.ActionSupport; +import com.opensymphony.xwork2.Preparable; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class LoginAction extends ActionSupport implements Preparable +{ + + private static final long serialVersionUID = 1L; + private static final transient Logger log = LogManager.getLogger(LoginAction.class); + + private String username; + private String password; + private transient Subject shiroUser; + + @Override + public void prepare() throws Exception + { + shiroUser = SecurityUtils.getSubject(); + } + + @Override + public String execute() + { + String result = INPUT; + + if (shiroUser != null) + { + // Do some stuff with a Session + Session session = shiroUser.getSession(); + session.setAttribute("MyUsername", username); + log.info("Saving 'username' value to session [" + username + "]"); + + // let's login the current user so we can check against roles and permissions: + if (! shiroUser.isAuthenticated()) + { + UsernamePasswordToken token = new UsernamePasswordToken(username, password); + token.setRememberMe(true); + try + { + shiroUser.login(token); + result = SUCCESS; + } + catch (UnknownAccountException uae) + { + addActionError("There is no user with username of '" + token.getPrincipal() + "'"); + log.error(uae.getMessage()); + } + catch (IncorrectCredentialsException ice) + { + addActionError("Password for account '" + token.getPrincipal() + "' was incorrect!"); + log.error(ice.getMessage()); + } + catch (LockedAccountException lae) + { + addActionError("The account for username '" + token.getPrincipal() + "' is locked. " + + "Please contact your administrator to unlock it."); + log.error(lae.getMessage()); + } + // ... catch more exceptions here (maybe custom ones specific to your application? + catch (AuthenticationException ae) + { + addActionError("An authentication exception has occurred trying to login user: " + token.getPrincipal()); + log.error(ae.getMessage()); + } + } + else if (shiroUser.isAuthenticated()) + { + result = SUCCESS; + } + } + + return result; + } + + public Subject getShiroUser() + { + return shiroUser; + } + + public void setShiroUser(Subject shiroUser) + { + this.shiroUser = shiroUser; + } + + public String getUsername() + { + return username; + } + + public void setUsername(String username) + { + this.username = username; + } + + public String getPassword() + { + return password; + } + + public void setPassword(String password) + { + this.password = password; + } + +} http://git-wip-us.apache.org/repos/asf/struts-examples/blob/ee5a3e3a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/action/LogoutAction.java ---------------------------------------------------------------------- diff --git a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/action/LogoutAction.java b/shiro-basic/src/main/java/org/apache/struts2/shiro/example/action/LogoutAction.java new file mode 100644 index 0000000..defab7d --- /dev/null +++ b/shiro-basic/src/main/java/org/apache/struts2/shiro/example/action/LogoutAction.java @@ -0,0 +1,18 @@ +package org.apache.struts2.shiro.example.action; + +public class LogoutAction extends ShiroBaseAction +{ + private static final long serialVersionUID = 1L; + + @Override + public String execute() + { + if (isAuthenticated()) + { + getShiroUser().logout(); // isAuthenticated = true -> getShiroUser() != null + } + + return SUCCESS; + } + +} http://git-wip-us.apache.org/repos/asf/struts-examples/blob/ee5a3e3a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/action/ShiroBaseAction.java ---------------------------------------------------------------------- diff --git a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/action/ShiroBaseAction.java b/shiro-basic/src/main/java/org/apache/struts2/shiro/example/action/ShiroBaseAction.java new file mode 100644 index 0000000..e93c145 --- /dev/null +++ b/shiro-basic/src/main/java/org/apache/struts2/shiro/example/action/ShiroBaseAction.java @@ -0,0 +1,28 @@ +package org.apache.struts2.shiro.example.action; + +import org.apache.shiro.subject.Subject; + +import com.opensymphony.xwork2.ActionSupport; + + +public class ShiroBaseAction extends ActionSupport +{ + private static final long serialVersionUID = 1L; + + private transient Subject shiroUser; + + public boolean isAuthenticated() + { + return shiroUser != null && shiroUser.isAuthenticated(); + } + + public Subject getShiroUser() + { + return shiroUser; + } + + public void setShiroUser(Subject shiroUser) + { + this.shiroUser = shiroUser; + } +} http://git-wip-us.apache.org/repos/asf/struts-examples/blob/ee5a3e3a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/action/WelcomeAction.java ---------------------------------------------------------------------- diff --git a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/action/WelcomeAction.java b/shiro-basic/src/main/java/org/apache/struts2/shiro/example/action/WelcomeAction.java new file mode 100644 index 0000000..b76bcfc --- /dev/null +++ b/shiro-basic/src/main/java/org/apache/struts2/shiro/example/action/WelcomeAction.java @@ -0,0 +1,107 @@ +package org.apache.struts2.shiro.example.action; + +import org.apache.shiro.session.Session; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class WelcomeAction extends ShiroBaseAction +{ + private static final long serialVersionUID = 1L; + private static final Logger log = LogManager.getLogger(WelcomeAction.class); + + private String username; + + @Override + public String execute() + { + String result = ERROR; + + if (isAuthenticated()) + { + // Retrieve value from session + Session session = getShiroUser().getSession(); + String value = (String) session.getAttribute("MyUsername"); + if (value.equals(username)) + { + log.info("Retrieved the correct 'username' value [" + value + "] from session"); + } + + printRoles(); + printPermissions(); + result = SUCCESS; + } + + return result; + } + + public void printRoles() + { + if (getShiroUser().hasRole("admin")) + { + log.info("User '" + username + "' has role of 'admin'"); + } + else + { + log.info("User '" + username + "' is missing role 'admin'"); + } + + if (getShiroUser().hasRole("schwartz")) + { + log.info("User '" + username + "' has role of 'schwartz'"); + } + else + { + log.info("User '" + username + "' is missing role 'schwartz'"); + } + + if (getShiroUser().hasRole("goodguy")) + { + log.info("User '" + username + "' has role of 'goodguy'"); + } + else + { + log.info("User '" + username + "' is missing role 'goodguy'"); + } + } + + public void printPermissions() + { + if (getShiroUser().isPermitted("lightsaber")) + { + log.info("User '" + username + "' has 'lightsaber' permission"); + } + else + { + log.info("User '" + username + "' is missing permission 'lightsaber'"); + } + + if (getShiroUser().isPermitted("winnebago")) + { + log.info("User '" + username + "' has 'winnebago' permission"); + } + else + { + log.info("User '" + username + "' is missing permission 'winnebago'"); + } + + if (getShiroUser().isPermitted("winnebago:drive:eagle5")) + { + log.info("User '" + username + "' has 'winnebago:drive:eagle5' permission"); + } + else + { + log.info("User '" + username + "' is missing permission 'winnebago:drive:eagle5'"); + } + } + + public String getUsername() + { + return username; + } + + public void setUsername(String username) + { + this.username = username; + } + +} http://git-wip-us.apache.org/repos/asf/struts-examples/blob/ee5a3e3a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/interceptor/ShiroUserInterceptor.java ---------------------------------------------------------------------- diff --git a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/interceptor/ShiroUserInterceptor.java b/shiro-basic/src/main/java/org/apache/struts2/shiro/example/interceptor/ShiroUserInterceptor.java new file mode 100644 index 0000000..794422c --- /dev/null +++ b/shiro-basic/src/main/java/org/apache/struts2/shiro/example/interceptor/ShiroUserInterceptor.java @@ -0,0 +1,57 @@ +/** + * + */ +package org.apache.struts2.shiro.example.interceptor; + +import org.apache.shiro.SecurityUtils; +import org.apache.shiro.subject.Subject; + +import com.opensymphony.xwork2.ActionInvocation; +import com.opensymphony.xwork2.interceptor.Interceptor; + +/** + * Inserts the current Shiro user into the value stack so that it can be + * injected into Struts 2 actions should they have a JavaBeans setter + * <code>setShiroUser(org.apache.shiro.subject.Subject shiroUser)</code>. + */ +public class ShiroUserInterceptor implements Interceptor { + + /** + * + */ + private static final long serialVersionUID = 1L; + + /* (non-Javadoc) + * @see com.opensymphony.xwork2.interceptor.Interceptor#destroy() + */ + @Override + public void destroy() + { + //release resources here + } + + /* (non-Javadoc) + * @see com.opensymphony.xwork2.interceptor.Interceptor#init() + */ + @Override + public void init() + { + // create resources here + } + + /* (non-Javadoc) + * @see com.opensymphony.xwork2.interceptor.Interceptor#intercept(com.opensymphony.xwork2.ActionInvocation) + */ + @Override + public String intercept(ActionInvocation actionInvocation) throws Exception + { + if (actionInvocation.getAction() instanceof org.apache.struts2.shiro.example.action.ShiroBaseAction) + { + Subject shiroUser = SecurityUtils.getSubject(); + actionInvocation.getStack().setValue("shiroUser", shiroUser); + } + + return actionInvocation.invoke(); + } + +} http://git-wip-us.apache.org/repos/asf/struts-examples/blob/ee5a3e3a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/struts2shiro/action/LoginAction.java ---------------------------------------------------------------------- diff --git a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/struts2shiro/action/LoginAction.java b/shiro-basic/src/main/java/org/apache/struts2/shiro/example/struts2shiro/action/LoginAction.java deleted file mode 100644 index ebaf866..0000000 --- a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/struts2shiro/action/LoginAction.java +++ /dev/null @@ -1,113 +0,0 @@ -package org.apache.struts2.shiro.example.action; - -import org.apache.shiro.SecurityUtils; -import org.apache.shiro.authc.AuthenticationException; -import org.apache.shiro.authc.IncorrectCredentialsException; -import org.apache.shiro.authc.LockedAccountException; -import org.apache.shiro.authc.UnknownAccountException; -import org.apache.shiro.authc.UsernamePasswordToken; -import org.apache.shiro.session.Session; -import org.apache.shiro.subject.Subject; -import com.opensymphony.xwork2.ActionSupport; -import com.opensymphony.xwork2.Preparable; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -public class LoginAction extends ActionSupport implements Preparable -{ - - private static final long serialVersionUID = 1L; - private static final transient Logger log = LogManager.getLogger(LoginAction.class); - - private String username; - private String password; - private Subject shiroUser; - - @Override - public void prepare() throws Exception - { - shiroUser = SecurityUtils.getSubject(); - } - - @Override - public String execute() - { - String result = INPUT; - - // Do some stuff with a Session - Session session = shiroUser.getSession(); - session.setAttribute("MyUsername", username); - log.info("Saving 'username' value to session [" + username + "]"); - - // let's login the current user so we can check against roles and permissions: - if (shiroUser != null && ! shiroUser.isAuthenticated()) - { - UsernamePasswordToken token = new UsernamePasswordToken(username, password); - token.setRememberMe(true); - try - { - shiroUser.login(token); - result = SUCCESS; - } - catch (UnknownAccountException uae) - { - addActionError("There is no user with username of '" + token.getPrincipal() + "'"); - log.error(uae.getMessage()); - } - catch (IncorrectCredentialsException ice) - { - addActionError("Password for account '" + token.getPrincipal() + "' was incorrect!"); - log.error(ice.getMessage()); - } - catch (LockedAccountException lae) - { - addActionError("The account for username '" + token.getPrincipal() + "' is locked. " + - "Please contact your administrator to unlock it."); - log.error(lae.getMessage()); - } - // ... catch more exceptions here (maybe custom ones specific to your application? - catch (AuthenticationException ae) - { - addActionError("An authentication exception has occurred trying to login user: " + token.getPrincipal()); - log.error(ae.getMessage()); - } - } - else if (shiroUser.isAuthenticated()) - { - result = SUCCESS; - } - - return result; - } - - public Subject getShiroUser() - { - return shiroUser; - } - - public void setShiroUser(Subject shiroUser) - { - this.shiroUser = shiroUser; - } - - public String getUsername() - { - return username; - } - - public void setUsername(String username) - { - this.username = username; - } - - public String getPassword() - { - return password; - } - - public void setPassword(String password) - { - this.password = password; - } - -} http://git-wip-us.apache.org/repos/asf/struts-examples/blob/ee5a3e3a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/struts2shiro/action/LogoutAction.java ---------------------------------------------------------------------- diff --git a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/struts2shiro/action/LogoutAction.java b/shiro-basic/src/main/java/org/apache/struts2/shiro/example/struts2shiro/action/LogoutAction.java deleted file mode 100644 index defab7d..0000000 --- a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/struts2shiro/action/LogoutAction.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.apache.struts2.shiro.example.action; - -public class LogoutAction extends ShiroBaseAction -{ - private static final long serialVersionUID = 1L; - - @Override - public String execute() - { - if (isAuthenticated()) - { - getShiroUser().logout(); // isAuthenticated = true -> getShiroUser() != null - } - - return SUCCESS; - } - -} http://git-wip-us.apache.org/repos/asf/struts-examples/blob/ee5a3e3a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/struts2shiro/action/ShiroBaseAction.java ---------------------------------------------------------------------- diff --git a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/struts2shiro/action/ShiroBaseAction.java b/shiro-basic/src/main/java/org/apache/struts2/shiro/example/struts2shiro/action/ShiroBaseAction.java deleted file mode 100644 index b5702fa..0000000 --- a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/struts2shiro/action/ShiroBaseAction.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.apache.struts2.shiro.example.action; - -import org.apache.shiro.subject.Subject; - -import com.opensymphony.xwork2.ActionSupport; - - -public class ShiroBaseAction extends ActionSupport -{ - private static final long serialVersionUID = 1L; - - private Subject shiroUser; - - public boolean isAuthenticated() - { - return shiroUser != null && shiroUser.isAuthenticated(); - } - - public Subject getShiroUser() - { - return shiroUser; - } - - public void setShiroUser(Subject shiroUser) - { - this.shiroUser = shiroUser; - } -} http://git-wip-us.apache.org/repos/asf/struts-examples/blob/ee5a3e3a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/struts2shiro/action/WelcomeAction.java ---------------------------------------------------------------------- diff --git a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/struts2shiro/action/WelcomeAction.java b/shiro-basic/src/main/java/org/apache/struts2/shiro/example/struts2shiro/action/WelcomeAction.java deleted file mode 100644 index 797df1d..0000000 --- a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/struts2shiro/action/WelcomeAction.java +++ /dev/null @@ -1,101 +0,0 @@ -package org.apache.struts2.shiro.example.action; - -import org.apache.shiro.session.Session; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -public class WelcomeAction extends ShiroBaseAction -{ - private static final long serialVersionUID = 1L; - private static final Logger log = LogManager.getLogger(WelcomeAction.class); - - private String username; - - @Override - public String execute() - { - String result = ERROR; - - if (isAuthenticated()) - { - // Retrieve value from session - Session session = getShiroUser().getSession(); - String value = (String) session.getAttribute("MyUsername"); - if (value.equals(username)) { - log.info("Retrieved the correct 'username' value [" + value + "] from session"); - } - - printRoles(); - printPermissions(); - result = SUCCESS; - } - - return result; - } - - public void printRoles() - { - if (getShiroUser().hasRole("admin")) - { - log.info("User '" + username + "' has role of 'admin'"); - } - else - { - log.info("User '" + username + "' is missing role 'admin'"); - } - if (getShiroUser().hasRole("schwartz")) - { - log.info("User '" + username + "' has role of 'schwartz'"); - } - else - { - log.info("User '" + username + "' is missing role 'schwartz'"); - } - if (getShiroUser().hasRole("goodguy")) - { - log.info("User '" + username + "' has role of 'goodguy'"); - } - else - { - log.info("User '" + username + "' is missing role 'goodguy'"); - } - } - - public void printPermissions() - { - if (getShiroUser().isPermitted("lightsaber")) - { - log.info("User '" + username + "' has 'lightsaber' permission"); - } - else - { - log.info("User '" + username + "' is missing permission 'lightsaber'"); - } - if (getShiroUser().isPermitted("winnebago")) - { - log.info("User '" + username + "' has 'winnebago' permission"); - } - else - { - log.info("User '" + username + "' is missing permission 'winnebago'"); - } - if (getShiroUser().isPermitted("winnebago:drive:eagle5")) - { - log.info("User '" + username + "' has 'winnebago:drive:eagle5' permission"); - } - else - { - log.info("User '" + username + "' is missing permission 'winnebago:drive:eagle5'"); - } - } - public String getUsername() - { - return username; - } - - public void setUsername(String username) - { - this.username = username; - } - -} http://git-wip-us.apache.org/repos/asf/struts-examples/blob/ee5a3e3a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/struts2shiro/interceptor/ShiroUserInterceptor.java ---------------------------------------------------------------------- diff --git a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/struts2shiro/interceptor/ShiroUserInterceptor.java b/shiro-basic/src/main/java/org/apache/struts2/shiro/example/struts2shiro/interceptor/ShiroUserInterceptor.java deleted file mode 100644 index 794422c..0000000 --- a/shiro-basic/src/main/java/org/apache/struts2/shiro/example/struts2shiro/interceptor/ShiroUserInterceptor.java +++ /dev/null @@ -1,57 +0,0 @@ -/** - * - */ -package org.apache.struts2.shiro.example.interceptor; - -import org.apache.shiro.SecurityUtils; -import org.apache.shiro.subject.Subject; - -import com.opensymphony.xwork2.ActionInvocation; -import com.opensymphony.xwork2.interceptor.Interceptor; - -/** - * Inserts the current Shiro user into the value stack so that it can be - * injected into Struts 2 actions should they have a JavaBeans setter - * <code>setShiroUser(org.apache.shiro.subject.Subject shiroUser)</code>. - */ -public class ShiroUserInterceptor implements Interceptor { - - /** - * - */ - private static final long serialVersionUID = 1L; - - /* (non-Javadoc) - * @see com.opensymphony.xwork2.interceptor.Interceptor#destroy() - */ - @Override - public void destroy() - { - //release resources here - } - - /* (non-Javadoc) - * @see com.opensymphony.xwork2.interceptor.Interceptor#init() - */ - @Override - public void init() - { - // create resources here - } - - /* (non-Javadoc) - * @see com.opensymphony.xwork2.interceptor.Interceptor#intercept(com.opensymphony.xwork2.ActionInvocation) - */ - @Override - public String intercept(ActionInvocation actionInvocation) throws Exception - { - if (actionInvocation.getAction() instanceof org.apache.struts2.shiro.example.action.ShiroBaseAction) - { - Subject shiroUser = SecurityUtils.getSubject(); - actionInvocation.getStack().setValue("shiroUser", shiroUser); - } - - return actionInvocation.invoke(); - } - -}