NutzCN Logo
问答 会话状态下,Mvcs.getLocalizationKey()获取为空
发布于 2607天前 作者 qq_895c9d88 1905 次浏览 复制 上一个帖子 下一个帖子
标签:

MainModule已经配置:
@Localization(value="msg/", defaultLocalizationKey="zh_CN")
public class MainModule {
......
}
1. 用户登录后,默认设置
Mvcs.setLocalizationKey(“zh_CN”);

  1. 上传文件接口中(确认当前还在会话中),Mvcs.getLocalizationKey()获取为空

请问是否还需要其它配置呢?

nutz版本为:1.r.60

13 回复

不会吧, setLocalizationKey事实上就是往session放个attr, getLocalizationKey就反过来

是不是用了shiro session呢?

哦,是使用了shiro session了,这个应该怎么破?

没有加,请问这个@SessioBy有资料么?

就MainModule加一句

@SessionBy(ShiroSessionProvider.class)

然后shiro.ini开头加个

sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager

@wendal
检查了MainModule

@Modules(value={CoreModule.class},scanPackage = true)
@SessionBy(ShiroSessionProvider.class)
@SetupBy(MvcSetup.class)
@IocBy(type = ComboIocProvider.class, args = { "*json", "com//view", "/ioc", "/provider",  "*anno", "com", "*tx", "*org.nutz.integration.quartz.QuartzIocLoader" })
@Fail(">>:/admin/common/unauthorized.rk")
@Localization(value="msg/", defaultLocalizationKey="zh_CN")
@Encoding(input = "UTF-8", output = "UTF-8")
@ChainBy(type = NutActionChainMaker.class, args = { "com/mvc-chains.js" })
@Views({ FreemarkerViewMaker.class })
public class MainModule {

}

是有使用@SessioBy的
ShiroSessionProvider代码:

public class ShiroSessionProvider implements SessionProvider {

	public HttpServletRequest filter(HttpServletRequest req, HttpServletResponse resp, ServletContext servletContext) {
		if ("OPTIONS".equalsIgnoreCase(req.getMethod())) {
			resp.addHeader("Access-Control-Allow-Origin", "*");
			resp.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Key");
		}
		if (req instanceof ShiroHttpServletRequest)
			return req;
		return new ShiroHttpServletRequest(req, servletContext, true);
	}

	public void notifyStop() {
	}

}

shiro.ini相关配置:

authc.loginUrl  = /user/login
logout.redirectUrl= /admin/user/login
sessionIdCookie=org.apache.shiro.web.servlet.SimpleCookie
sessionIdCookie.name=rk
#sessionIdCookie.domain=rekoe.com
#sessionIdCookie.path=
sessionIdCookie.maxAge=1800
sessionIdCookie.httpOnly=true

sessionManager=org.apache.shiro.web.session.mgt.ServletContainerSessionManager
securityManager.sessionManager=$sessionManager

sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
sha256Matcher.storedCredentialsHexEncoded = false
sha256Matcher.hashIterations = 1024
sha256Matcher.hashSalted = true
#nutzAuthoRealm = com.rekoe.shiro.realm.NutAuthoDaoRealm
shiroDbRealm = com.go.shiro.realm.NutDaoRealm
shiroDbRealm.credentialsMatcher = $sha256Matcher
securityManager.realms = $shiroDbRealm
authcStrategy = com.go.shiro.authc.pam.AnySuccessfulStrategy  
securityManager.authenticator.authenticationStrategy = $authcStrategy

cacheManager = org.apache.shiro.cache.ehcache.EhCacheManager
cacheManager.cacheManagerConfigFile=classpath:ehcache.xml
securityManager.cacheManager = $cacheManager

那就奇怪, 登录和上传的时候分别打印一下session.getId()看看

@wendal sessionid是一样的

2017-11-02 16:14:27,593 [ERROR][com.go.filter.CaptchaFormAuthenticationFilter] - 登录:sessionID:E6DE44531FBD79CAE42D369BD806BFA6
2017-11-02 16:14:30,847 [ERROR][com.go.module.admin.AdminLoginAct] - qihao.lin login
2017-11-02 16:14:35,235 [WARN][org.nutz.dao.impl.entity.AnnotationEntityMaker] - No @Table found, fallback to use table name='organization_column' for type 'com.go.utils.columns.factory.OrganizationColumn'
2017-11-02 16:14:35,236 [ERROR][com.go.service.BaseService] - getObjListBySql ttl9rrfsraj5lpipgsgiggmapd start 
2017-11-02 16:14:35,246 [WARN][org.nutz.dao.impl.entity.AnnotationEntityMaker] - No @Table found, fallback to use table name='factory_column' for type 'com.go.utils.columns.factory.FactoryColumn'
2017-11-02 16:14:35,301 [ERROR][com.go.service.BaseService] - getObjListBySql ttl9rrfsraj5lpipgsgiggmapd end 65
2017-11-02 16:14:35,305 [ERROR][com.go.service.BaseService] - getObjListBySql tegcvs56mmi0jofareu546dcfc start 
2017-11-02 16:14:35,582 [ERROR][com.go.service.BaseService] - getObjListBySql tegcvs56mmi0jofareu546dcfc end 277
2017-11-02 16:14:40,184 [ERROR][com.go.service.BaseService] - getObjListBySql 1t46ckpfa0gl4ocfvruepirb7m start 
2017-11-02 16:14:40,187 [ERROR][com.go.service.BaseService] - getObjListBySql 1t46ckpfa0gl4ocfvruepirb7m end 3
2017-11-02 16:14:47,049 [ERROR][schedule.go.module.isp.IspDeliveredAct] - lang:zh_CN
2017-11-02 16:14:47,050 [ERROR][schedule.go.module.isp.IspDeliveredAct] - 上传sessionID:E6DE44531FBD79CAE42D369BD806BFA6

eclispe监听Mvcs.getLocalizationKey(),一直是error的
如需截图,可以qq发给您

    public static boolean setLocalizationKey(String key) {
        HttpSession sess = getHttpSession();
        if (null == sess)
            return false;
        sess.setAttribute(LOCALE_KEY, key);
        return true;
    }

难道是setLocalizationKey返回false了?

刚测试,返回true,
在上传接口中,我上面的日记对应代码如下:

String lang = (String) req.getSession().getAttribute("nutz_mvc_localization_key");
			log.error("lang:"+lang);
			log.error("上传sessionID:"+req.getSession().getId());
2017-11-02 16:14:47,049 [ERROR][schedule.go.module.isp.IspDeliveredAct] - lang:zh_CN
2017-11-02 16:14:47,050 [ERROR][schedule.go.module.isp.IspDeliveredAct] - 上传sessionID:E6DE44531FBD79CAE42D369BD806BFA6

... 不会这么神奇吧 Mvcs.getLocalizationKey() 也是走getAttribute的

@wendal 找到原因了
当前这个项目配置的nutz版本是1.r.60,而maven配置还引用另外一个项目,它的nutz版本是1.r.61
将当前这个项目的nutz版本修改为1.r.61,就好了。

经验+1

... 版本冲突了...

添加回复
请先登陆
回到顶部