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

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

1.创建maven项目:
第一步:
IDEA上搭建maven项目整合springboot+mybatis+shiro_第1张图片
第二步:注意我是选择了自己的maven仓库
IDEA上搭建maven项目整合springboot+mybatis+shiro_第2张图片
第三步:
IDEA上搭建maven项目整合springboot+mybatis+shiro_第3张图片

2.配置文件 ,创建完成之后的目录格式;
如下图标注 1 所示,.idea 即为 Project 的配置文件目录。
如下图标注 2 所示,.iml 即为 Module 的配置文件。
通过上面的了解我们也知道 IntelliJ IDEA 项目的配置变动都是以这些 XML 文件的方式来表现的,所以我们也可以通过了解这些 XML 文件来了解 IntelliJ IDEA 的一些配置。也因为此特性,所以如果在项目协同中,我们要保证所有的项目配置一致,就可以考虑把这些配置文件上传到版本控制中(包括 .idea 目录和 .iml 文件)。如果把这些文件加入到版本控制之后,那又有一点是需要考虑的,那就是协同者 Checkout 项目下来之后,按自己的需求进行项目配置的之后,项目的 XML 文件也会跟着变化。
IDEA上搭建maven项目整合springboot+mybatis+shiro_第4张图片
这里注意,idea的这里不会为我们创建java项目,需要我们手动创建java文件夹并设置为sources文件
IDEA上搭建maven项目整合springboot+mybatis+shiro_第5张图片
开始配置POM文件:


org.springframework.boot
spring-boot-starter-parent
1.5.4.RELEASE




org.springframework.boot
spring-boot-starter-web



org.springframework.boot
spring-boot-configuration-processor
true



mysql
mysql-connector-java



org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1




javax.servlet
javax.servlet-api



javax.servlet
jstl



org.apache.tomcat.embed
tomcat-embed-jasper



org.apache.shiro
shiro-core
1.2.2


org.apache.shiro
shiro-spring
1.2.2



org.apache.shiro
shiro-ehcache
1.2.2




com.zaxxer
HikariCP-java6
2.3.9
compile



com.baidu
ueditor
1.1.2



commons-codec
commons-codec
1.9



org.hibernate
hibernate-validator
5.1.3.Final



javax.mail
mail
1.4


org.json
json
20160810


commons-fileupload
commons-fileupload
1.3.3



com.fasterxml.jackson.core
jackson-databind
2.4.2


com.github.abel533
mapper
${mapper.version}




org.apache.poi
poi
3.9


org.apache.poi
poi-scratchpad
3.9


org.apache.poi
poi-ooxml
3.9

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

3.springboot整合shiro不像spring那样简单的配置xml文件。但是原理都是一样的
IDEA上搭建maven项目整合springboot+mybatis+shiro_第6张图片
这是配置类中的所有类,这里没有定义缓存,因为我并不需要,但最后还是贴上缓存代码

 //配置核心安全事务管理器
    @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 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  

你可能感兴趣的:(IDEA上搭建maven项目整合springboot+mybatis+shiro)