您的位置:首页 > 编程语言 > Java开发

IDEA上搭建maven项目整合springboot+mybatis+shiro

2017-07-17 23:19 1271 查看
IDEA上搭建maven项目整合springboot+mybatis+shiro

1.创建maven项目:

第一步:



第二步:注意我是选择了自己的maven仓库



第三步:



2.配置文件 ,创建完成之后的目录格式;

如下图标注 1 所示,.idea 即为 Project 的配置文件目录。

如下图标注 2 所示,.iml 即为 Module 的配置文件。

通过上面的了解我们也知道 IntelliJ IDEA 项目的配置变动都是以这些 XML 文件的方式来表现的,所以我们也可以通过了解这些 XML 文件来了解 IntelliJ IDEA 的一些配置。也因为此特性,所以如果在项目协同中,我们要保证所有的项目配置一致,就可以考虑把这些配置文件上传到版本控制中(包括 .idea 目录和 .iml 文件)。如果把这些文件加入到版本控制之后,那又有一点是需要考虑的,那就是协同者 Checkout 项目下来之后,按自己的需求进行项目配置的之后,项目的 XML 文件也会跟着变化。



这里注意,idea的这里不会为我们创建java项目,需要我们手动创建java文件夹并设置为sources文件



开始配置POM文件:

<!--springboot依赖包,官网可下载-->

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.5.4.RELEASE</version>

<relativePath/> <!-- lookup parent from repository -->

</parent>

<!--springboot包,官网可下载-->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!--引入配置文件-->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-configuration-processor</artifactId>

<optional>true</optional>

</dependency>

<!--添加mysql的组件-->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

</dependency>

<!--整合mybatis-->

<dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>1.1.1</version>

</dependency>

<!--既然是web项目,下面这三个包是引导jsp用的,不要jsp就可以不要-->

<!--servlet依赖-->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

</dependency>

<!--jstl依赖-->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jstl</artifactId>

</dependency>

<!--使jsp页面生效-->

<dependency>

<groupId>org.apache.tomcat.embed</groupId>

<artifactId>tomcat-embed-jasper</artifactId>

</dependency>

<!-- shiro spring. -->

<dependency>

<groupId>org.apache.shiro</groupId>

<artifactId>shiro-core</artifactId>

<version>1.2.2</version>

</dependency>

<dependency>

<groupId>org.apache.shiro</groupId>

<artifactId>shiro-spring</artifactId>

<version>1.2.2</version>

</dependency>

<!-- shiro ehcache -->

<dependency>

<groupId>org.apache.shiro</groupId>

<artifactId>shiro-ehcache</artifactId>

<version>1.2.2</version>

</dependency>

<!--还有一些不需要的包,放在这里算是备份吧-->

<!--超高性能连接池-->

<dependency>

<groupId>com.zaxxer</groupId>

<artifactId>HikariCP-java6</artifactId>

<version>2.3.9</version>

<scope>compile</scope>

</dependency>

<!--百度插件依赖-->

<dependency>

<groupId>com.baidu</groupId>

<artifactId>ueditor</artifactId>

<version>1.1.2</version>

</dependency>

<!-- 字符加密、解密 -->

<dependency>

<groupId>commons-codec</groupId>

<artifactId>commons-codec</artifactId>

<version>1.9</version>

</dependency>

<!-- 数据校验 -->

<dependency>

<groupId>org.hibernate</groupId>

<artifactId>hibernate-validator</artifactId>

<version>5.1.3.Final</version>

</dependency>

<!-- 邮件 -->

<dependency>

<groupId>javax.mail</groupId>

<artifactId>mail</artifactId>

<version>1.4</version>

</dependency>

<dependency>

<groupId>org.json</groupId>

<artifactId>json</artifactId>

<version>20160810</version>

</dependency>

<dependency>

<groupId>commons-fileupload</groupId>

<artifactId>commons-fileupload</artifactId>

<version>1.3.3</version>

</dependency>

<!-- Jackson Json处理工具包 -->

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>2.4.2</version>

</dependency>

<dependency>

<groupId>com.github.abel533</groupId>

<artifactId>mapper</artifactId>

<version>${mapper.version}</version>

</dependency>

<!-- POI -->

<!-- https
4000
://mvnrepository.com/artifact/org.apache.poi/poi -->

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi</artifactId>

<version>3.9</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-scratchpad</artifactId>

<version>3.9</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml</artifactId>

<version>3.9</version>

</dependency>


这些包并不是全部都需要,但是这里写得比较全,也算是为自己存个备份。

3.springboot整合shiro不像spring那样简单的配置xml文件。但是原理都是一样的



这是配置类中的所有类,这里没有定义缓存,因为我并不需要,但最后还是贴上缓存代码

//配置核心安全事务管理器
@Bean(name="securityManager")
public DefaultWebSecurityManager securityManager(@Qualifier("authRealm") AuthRealm authRealm) {
System.err.println("--------------shiro已经加载----------------");
DefaultWebSecurityManager manager=new DefaultWebSecurityManager();
manager.setRealm(authRealm);
return manager;
}
//过滤器
@Bean(name="shiroFilter")
public ShiroFilterFactoryBean shiroFilter(@Qualifier("securityManager") DefaultWebSecurityManager manager) {
ShiroFilterFactoryBean bean=new ShiroFilterFactoryBean();
bean.setSecurityManager(manager);
//配置登录的url和登录成功的url
bean.setLoginUrl("/index.jsp");
bean.setSuccessUrl("/home");
//配置访问权限  拦截策略以键值对存入map
LinkedHashMap<String, String> filterChainDefinitionMap=new LinkedHashMap<>();//必须LinkedHashMap
filterChainDefinitionMap.put("/jsp/login.jsp*", "anon"); //表示可以匿名访问
filterChainDefinitionMap.put("/**", "anon"); //表示可以匿名访问
bean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return bean;
}
//配置自定义的权限登录器  没有自定义的类就小心配置。我这里是自定义配置类了
@Bean(name="authRealm")
public AuthRealm authRealm(@Qualifier("credentialsMatcher") CredentialsMatcher matcher) {
AuthRealm authRealm=new AuthRealm();
authRealm.setCredentialsMatcher(matcher);
return authRealm;
}
//配置自定义的密码比较器   如果没有自定义密码则不需要
@Bean(name="credentialsMatcher")
public CredentialsMatcher credentialsMatcher() {
return new AuthCredential();
}
//将生命周期交给springboot管理
@Bean
public LifecycleBeanPostProcessor lifecycleBeanPostProcessor(){
return new LifecycleBeanPostProcessor();
}
//强制使用cglib创建代理对象
@Bean
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
DefaultAdvisorAutoProxyCreator creator=new DefaultAdvisorAutoProxyCreator();
creator.setProxyTargetClass(true);
return creator;
}
//核心安全配适器,必须的
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager") DefaultWebSecurityManager manager) {
AuthorizationAttributeSourceAdvisor advisor=new AuthorizationAttributeSourceAdvisor();
advisor.setSecurityManager(manager);
return advisor;
}


这是配置类中的所有类,这里没有定义缓存,因为我并不需要,如果需要配置缓存,则要配置缓存类,并且写缓存配置文件。网上一搜一大把

关于mybatis的配置,xml跟spring一样,拷贝过来就行,配置不需要变化。在application中的配置:

mybatis:
type-aliases-package: org.hmwqly.lp.pojo
config-location: classpath:/mybatis/mybatis-config.xml
mapper-locations: classpath:/mybatis/mappers/*.xml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/lp
username: root
password: root
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息