博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC+Apache Shiro+JPA(hibernate)整合配置
阅读量:6452 次
发布时间:2019-06-23

本文共 13915 字,大约阅读时间需要 46 分钟。

序:

关于标题:

  说是教学,实在愧不敢当,但苦与本人文笔有限,实在找不到更合理,谦逊的词语表达,只能先这样定义了。

  其实最真实的想法,只是希望这个关键词能让更多的人浏览到这篇文章,也算是对于自己写文章的一个肯定吧。^_^!

关于内容:

  再写这系列文章之前,本人和许多人一样都是伸手党,并深深的了解咱伸手党且英文较差的朋友对于新知识的学习及获取中文资料少的痛苦。所以本着“取之于民,共享与民”的原则,记录下实际工作中对SpringMVC+Shiro整合应用的部分心得。本人技术水平有限,仅希望文章对他人有一定的参考价值,足矣。

关于拍砖:

  请轻拍,很痛的。且最好附上您的高见。

另:Shiro基础及原理,推荐学习,同时感谢他的博客,在我学习Shiro的过程中,给予很大帮助!

 


 

教学:

一、SpringMVC+Apache Shiro+JPA(hibernate)整合配置

(1)新建Web工程,且导入所需Jar包。(以下截图为真实项目中删减后保留,如有不需要的JAR包,请自行删除)

(2)配置web.xml,applicationContext.xml, spring-mvc.xml, log4j.properties

web.xml

contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
hibernateFilter
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
hibernateFilter
/*
shiroFilter
org.springframework.web.filter.DelegatingFilterProxy
targetFilterLifecycle
true
shiroFilter
/*
webAppRootKey
spring_springmvc_jpa.root
log4jConfigLocation
classpath:log4j.properties
org.springframework.web.util.Log4jConfigListener
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
characterEncodingFilter
/*
HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
HiddenHttpMethodFilter
dispatcherServlet
org.springframework.web.util.IntrospectorCleanupListener
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
dispatcherServlet
/
JSPConfiguration
*.jsp
true
utf-8
false
login.jsp

applicationContext.xml

org.hibernate.dialect.MySQL5Dialect
com.mysql.jdbc.Driver
3
18
10
update
true
false
none
/login = anon /validateCode = anon /** = authc

spring-mvc.xml

text/html;charset=UTF-8
/system/error

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/shirodemo?useUnicode=true&characterEncoding=UTF-8jdbc.username=rootjdbc.password=rootBoneCP.idleConnectionTestPeriod=60BoneCP.idleMaxAge=60BoneCP.maxConnectionsPerPartition=5BoneCP.minConnectionsPerPartition=1BoneCP.partitionCount=3BoneCP.acquireIncrement=2  BoneCP.statementsCacheSize=0   BoneCP.releaseHelperThreads=3

log4j.properties

log4j.rootLogger=INFO,stdout,filelog4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss,SSS} [%c]-[%p] %m%n log4j.appender.file=org.apache.log4j.DailyRollingFileAppender log4j.appender.file.File=${spring_springmvc_jpa.root}/shirodemo.loglog4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%nlog4j.logger.org.hibernate.tool.hbm2ddl=info

(3)建立JavaBean,User.java,Role.java,Permisson.java

User.java

package org.shiro.demo.entity;import java.io.Serializable;import java.util.Collection;import javax.persistence.CascadeType;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.JoinTable;import javax.persistence.ManyToMany;import javax.persistence.Table;import org.codehaus.jackson.annotate.JsonIgnore;@Entity@Table(name = "CMSUser")public class User implements Serializable {    private static final long serialVersionUID = 7419229779731522702L;    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    @Column(name = "userid")    private Long id;    @Column(length = 50, unique = true)    private String account;    @Column(length = 100)    @JsonIgnore    //springmvc生成json不包含此字段    private String password;    @Column(length = 50)    private String nickname;        @ManyToMany(cascade = {CascadeType.PERSIST })    @JsonIgnore    @JoinTable(name = "CMSUserRole", joinColumns = { @JoinColumn(name = "userid", referencedColumnName = "userid") },         inverseJoinColumns = { @JoinColumn(name = "roleid", referencedColumnName = "roleid") })    private Collection
roles; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public Collection
getRoles() { return roles; } public void setRoles(Collection
roles) { this.roles = roles; } /** * 本函数输出将作为默认的
输出. */ public String toString() { return account; }}

Role.java

package org.shiro.demo.entity;import java.io.Serializable;import java.util.Collection;import javax.persistence.Basic;import javax.persistence.CascadeType;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.JoinTable;import javax.persistence.ManyToMany;import javax.persistence.Table;@Entity@Table(name = "CMSRole")public class Role implements Serializable{    private static final long serialVersionUID = 6177417450707400228L;        @Id    @GeneratedValue(strategy=GenerationType.IDENTITY)    @Column(name="roleid")    private Long id;    @Column(length=50)    private String name;    @Column(length=50)    private String description;        @ManyToMany(mappedBy = "roles")    @Basic(fetch = FetchType.LAZY)    private Collection
users; @ManyToMany(cascade={CascadeType.PERSIST,CascadeType.REFRESH,CascadeType.MERGE}, fetch = FetchType.LAZY) @JoinTable(name = "CMSRolePms", joinColumns = { @JoinColumn(name = "roleid", updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "pmsid", updatable = false) }) private Collection
pmss; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Collection
getUsers() { return users; } public void setUsers(Collection
users) { this.users = users; } public Collection
getPmss() { return pmss; } public void setPmss(Collection
pmss) { this.pmss = pmss; }}

Permisson.java

package org.shiro.demo.entity;import java.io.Serializable;import java.util.Collection;import javax.persistence.CascadeType;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.ManyToMany;import javax.persistence.ManyToOne;import javax.persistence.OneToMany;import javax.persistence.Table;@Entity@Table(name="CMSPermission")public class Permission implements Serializable{    private static final long serialVersionUID = -8792590494605747957L;        @Id    @GeneratedValue(strategy=GenerationType.IDENTITY)    @Column(name="pmsid")    private Long id;    @Column(length=50)    private String name;    @Column(length=100)    private String description;    @Column(length=50)    private String permission;    @ManyToOne(fetch = FetchType.LAZY)    @JoinColumn(name = "parentid")    private Permission parent;        @OneToMany(mappedBy = "parent",fetch = FetchType.LAZY,cascade={CascadeType.ALL})    private Collection
children; @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "pmss") private Collection
roles; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getPermission() { return permission; } public void setPermission(String permission) { this.permission = permission; } public Permission getParent() { return parent; } public void setParent(Permission parent) { this.parent = parent; } public Collection
getChildren() { return children; } public void setChildren(Collection
children) { this.children = children; } public Collection
getRoles() { return roles; } public void setRoles(Collection
roles) { this.roles = roles; }}

至此,配置基本完成,请继续学习下一篇文章:

  

 

转载自:http://www.cnblogs.com/xql4j/archive/2013/03/30/2990841.html

 

转载于:https://www.cnblogs.com/guochunguang/p/3755228.html

你可能感兴趣的文章
Python算法(含源代码下载)
查看>>
利用Windows自带的Certutil查看文件MD5
查看>>
通过原生js添加div和css
查看>>
简单的导出表格和将表格下载到桌面上。
查看>>
《ArcGIS Engine+C#实例开发教程》第一讲桌面GIS应用程序框架的建立
查看>>
递归查询上一级
查看>>
JAVA - 大数类详解
查看>>
查询指定名称的文件
查看>>
批处理文件
查看>>
1.每次按一下pushbutton控件,切换图片?
查看>>
Python 嵌套列表解析
查看>>
[GXOI/GZOI2019]旧词——树链剖分+线段树
查看>>
android 补间动画的实现
查看>>
2017年广东省ACM省赛(GDCPC-2017)总结
查看>>
第十届蓝桥杯B组C++题目详解和题型总结
查看>>
树的存储结构2 - 数据结构和算法42
查看>>
函数的嵌套调用
查看>>
OC中使用 static 、 extern、 const使用
查看>>
简单理解函数回调——同步回调与异步回调
查看>>
POJ 1007
查看>>