【JeeSite】登录和主题切换

最高管理员账号,用户名:thinkgem 密码:admin

1.    密码加密:登录用户密码进行SHA1散列加密,此加密方法是不可逆的。保证密文泄露后的安全问题。

在spring-shiro配置文件中找到<property name="realm" ref="systemAuthorizingRealm" />,参考小峰项目的登录时shiro会调用此验证密码

SystemService.java 密码检验方法SystemService.validatePassword(vPassword, user.getPassword());

生成安全的密码 entryptPassword(String plainPassword);

/**
     * 认证回调函数, 登录时调用
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) {
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
        
        int activeSessionSize = getSystemService().getSessionDao().getActiveSessions(false).size();
        if (logger.isDebugEnabled()){
            logger.debug("login submit, active session size: {}, username: {}", activeSessionSize, token.getUsername());
        }
        
        // 校验登录验证码
        if (LoginController.isValidateCodeLogin(token.getUsername(), false, false)){
            Session session = UserUtils.getSession();
            String code = (String)session.getAttribute(ValidateCodeServlet.VALIDATE_CODE);
            if (token.getCaptcha() == null || !token.getCaptcha().toUpperCase().equals(code)){
                throw new AuthenticationException("msg:验证码错误, 请重试.");
            }
        }
        
        // 校验用户名密码
        User user = getSystemService().getUserByLoginName(token.getUsername());
        if (user != null) {
            if (Global.NO.equals(user.getLoginFlag())){
                throw new AuthenticationException("msg:该已帐号禁止登录.");
            }
            byte[] salt = Encodes.decodeHex(user.getPassword().substring(0,16));
             //通常是与数据库中用户名和密码进行比对,这里就省略了
              //比对失败则抛出对应信息的异常AuthenticationException
            return new SimpleAuthenticationInfo(new Principal(user, token.isMobileLogin()), 
                    user.getPassword().substring(16), ByteSource.Util.bytes(salt), getName());
        } else {
            return null;
        }
    }
View Code

2. 登录成功跳转到return "redirect:" + adminPath;(跳转到管理页面@RequestMapping(value = "${adminPath}") , 即/a ,见jeesite.properties 中的配置,可以读取注入) ,basecontroller.java 注入值:

  /**
     * 管理基础路径
     */
    @Value("${adminPath}")
    protected String adminPath;   

所有/a/* 路径会被sitemesh 过滤,进入return "modules/sys/sysIndex"; 

sysIndex.jsp就是后台的页面框架,左边是菜单栏,右边是mainFrame。

为了简化读取properties文件中的配置值,Spring支持@Value注解的方式来获取,这种方式大大简化了项目的配置,业务中也提高了灵活性。@Value("${key}")使用,如下:

<!-- 加载配置属性文件 -->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:jeesite.properties" />
    
    <!-- 加载应用属性实例,可通过  @Value("#{APP_PROP['jdbc.driver']}") String jdbcDriver 方式引用 -->
    <util:properties id="APP_PROP" location="classpath:jeesite.properties" local-override="true"/>

===============web.xml=====================

<!-- SiteMesh -->
    <filter>
        <filter-name>sitemeshFilter</filter-name>
        <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>sitemeshFilter</filter-name>
        <url-pattern>/a/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>sitemeshFilter</filter-name>
        <url-pattern>/f/*</url-pattern>
    </filter-mapping>

==========================================
View Code

3.主题切换

    <li id="themeSwitch" class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#" title="主题切换"><i class="icon-th-large"></i></a>
                        <ul class="dropdown-menu">
                            <c:forEach items="${fns:getDictList('theme')}" var="dict"><li><a href="#" onclick="location='${pageContext.request.contextPath}/theme/${dict.value}?url='+location.href">${dict.label}</a></li></c:forEach>
                            <li><a href="javascript:cookie('tabmode','${tabmode eq '1' ? '0' : '1'}');location=location.href">${tabmode eq '1' ? '关闭' : '开启'}页签模式</a></li>
                        </ul>
                        <!--[if lte IE 6]><script type="text/javascript">$('#themeSwitch').hide();</script><![endif]-->
                    </li>
sysIndex.jsp

url=location.href 表示本地跳转

主题样式保存在cookie中

/**
     * 获取主题方案
     */
    @RequestMapping(value = "/theme/{theme}")
    public String getThemeInCookie(@PathVariable String theme, HttpServletRequest request, HttpServletResponse response){
        if (StringUtils.isNotBlank(theme)){
            CookieUtils.setCookie(response, "theme", theme);
        }else{
            theme = CookieUtils.getCookie(request, "theme");
        }
        return "redirect:"+request.getParameter("url");
    }
View Code

在head.jsp 加载cookie中保存的样式

<link href="${ctxStatic}/bootstrap/2.3.1/css_${not empty cookie.theme.value ? cookie.theme.value : 'cerulean'}/bootstrap.min.css" type="text/css" rel="stylesheet" />
原文地址:https://www.cnblogs.com/skyislimit/p/6744783.html