如何在没有登录页面的情况下使用Shiro进行WEB应用程序

时间:2018-07-02 10:54:30

标签: java struts2 shiro

我有一个用Struts2编写的WEB应用程序。而且它没有登录页面。登录页面是主页的div。 我想使用Shiro来保护我的应用程序。

没有登录页面,我不明白如何使用Shiro。 而且我不明白如何从Struts Action将用户名传递给Shiro。

请帮助我。 谢谢。

我的动作:

public class InternalLoginAction extends ActionSupport {

private String remoteUser;

public String execute() throws NamingException, SQLException {
    HttpServletRequest request = ServletActionContext.getRequest();
    remoteUser = request.getRemoteUser();
    if (remoteUser != null) {
        User user = new User();
        user.setId(user.calcIdTemp(remoteUser));
        user.setName(remoteUser);
        user.setCompanyId(LoginDao.getUserCompanyId(remoteUser.toUpperCase()));
        user.setExternal(false);
        user.saveSession();

        //I want to pass username to Shiro here.

    }
    return SUCCESS;
}

public String getRemoteUser() {
    return remoteUser;
}

public void setRemoteUser(String remoteUser) {
    this.remoteUser = remoteUser;
}

}

很抱歉,我无法显示所有页面。只有这样:

<div id="login-dialog" title="Authorization" class="hide">
  <form id="login-dialog-form" >
  <table>
    <tr>
      <td>Login:</td>
      <td>
        <input id="login-dialog-login" name="form.login" size="22" maxlength="22" class="text ui-widget-content ui-corner-all" data-parsley-required="true" autofocus>
      </td>
    </tr>
    <tr>
      <td>Password:</td>
      <td>
       <input id="login-dialog-pass" name="form.pw" type="password" size="22" maxlength="22" class="text ui-widget-content ui-corner-all" data-parsley-required="true" data-parsley-minlength="6" data-parsley-maxlength="20">
      </td>
    </tr>
  </table>
  <br><a href="#" onclick="wsp.internalLogin()">I am employee of XXX</a>          
  </form>
</div>  

我的功能:

 internalLogin: function() {
$.ajax({
  url: '/xxx/public/internalLogin.do',
  type: 'POST',
  success: function(data) {
    location.reload(true);
  },
  error: function(request, textStatus, errorThrown) {
    wsp.alertMessage(request.responseText);
  }
});

1 个答案:

答案 0 :(得分:0)

您可能想在执行操作之前处理所有登录逻辑。

Shiro使用Servlet过滤器开箱即用。例如,此示例(仅是几个JSP)实际上并未处理任何登录逻辑: https://github.com/apache/shiro/tree/master/samples/servlet-plugin

除了呈现登录表单外。 Shiro Servlet过滤器会拦截登录页面(在这种情况下为/login.jsp)中的帖子,并自动处理其他所有内容。