1,懒加载的一系列问题
①, no Session
<!--解决no-session的问题-->
<filter>
<filter-name>openEntityManager</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openEntityManager</filter-name>
<url-pattern>/*
②,hibernate添加了一些字段(变成Json时报错)
解决方案一: 字段上加注解 @JsonIgnoreProperties(value={“hibernateLazyInitializer”,“handler”,“fieldHandler”})
解决方案二:自己配置(拷备过来用即可)
CustomMapper
public class CustomMapper extends ObjectMapper {
public CustomMapper() {
this.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// 设置 SerializationFeature.FAIL_ON_EMPTY_BEANS 为 false
this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
}
}
applicationContext-mvc.xml
<!-- Spring MVC 配置 -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json; charset=UTF-8</value>
<value>application/x-www-form-urlencoded; charset=UTF-8</value>
</list>
</property>
<!-- No serializer:配置 objectMapper 为我们自定义扩展后的 CustomMapper,解决了返回对象有关系对象的报错问题 -->
<property name="objectMapper">
<bean class="cn.itsource.aisell.common.CustomMapper"></bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
③,修改部门时出现 的 n-to-n 问题
持久化对象的ID都不能被修改的;所以在employee中的关联对象设置为空
2,shiro
轻量级的权限框架(Java安全对象)
shiro(轻量级,粗粒度) , Spring security(细粒度)
RBAC:权限(登录,授权) 用户(n)-角色(n)-权限(n)(资源
2.1 shiro的四大基石
身份认证(登录) Authentication
授权(权限) Authorization
密码学 Cryptography
会话管理 Session Management(支持所有)
2.2 重要的对象
Hello案例
建一个普通的maven项目
2.1 导包
<!--使用shiro需要先导包-->
<dependencies>
<!--shiro的核心包-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.0</version>
</dependency>
<!--日志包-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!--测试包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency>
</dependencies>
2.2 ini文件
文件中有咱们的用户角色权限
[users]
root = 123456, admin
guest = guest, it
[roles]
admin = *
it = employee:*
hr = employee:save
2.3 功能测试
主要测试登录,权限认证
@Test
public void testHello() throws Exception{
//①.拿到权限管理对象
/**
* 读取了shiro.ini的文件(隐藏了realm) -> 隐藏了iniRealm
* SecurityManager:权限管理器,shiro的所有功能都放在里面
*/
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();
//②.相当于把SecurityManager放到了当前上下文
/**
* 可以让我们在当前系统的任何位置都可以拿到SecurityManager对象
*/
SecurityUtils.setSecurityManager(securityManager);
//③.拿到当前用户(没有登录就是游客)
Subject currentUser = SecurityUtils.getSubject();
System.out.println("用户是否登录:"+currentUser.isAuthenticated());
//④.如果没有登录,让他进行登录
if(!currentUser.isAuthenticated()){
//ctrl+alt+t :包含代码
//4.1 准备令牌(对象) 用户名密码令牌
UsernamePasswordToken token = new UsernamePasswordToken("guest","guest");
//4.2 进行登录功能
currentUser.login(token);
System.out.println("用户是否登录:"+currentUser.isAuthenticated());
}