SpringBoot集成lombok工具

什么是lombok?

自动生成setget方法,构造函数,打印日志

官网:http://projectlombok.org/features/index.

平时我们写的一些重复代码,比如每个实体类的setter,getter方法,给每个类写上Logger获取的方法,这样写的话太繁琐,我们就可以使用lombok的工具去简化这个配置的操作。

首先先要在Eclipse下安装lombok,在控制台中,使用java -jar lombok.jar(lombok.jar 自己去网上下载)为eclipse安装lombok插件,安装成功后需要重启eclipse,在eclipse目录下可以看到lombok.jar这个jar包,然后我们打开eclipse.ini配置文件查看到如下代码就正确



然后我们需要在pom.xml中引入lombok的jar包了

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

部分注解说明,官网:http://projectlombok.org/features/index.

@Data   :注解在类上;提供类所有属性的 getter 和 setter 方法,此外还提供了equals、canEqual、hashCode、toString 方法
@Setter:注解在属性上;为属性提供 setting 方法
@Getter:注解在属性上;为属性提供 getting 方法
@Log4j :注解在类上;为类提供一个 属性名为log 的 log4j 日志对象
@NoArgsConstructor:注解在类上;为类提供一个无参的构造方法
@AllArgsConstructor:注解在类上;为类提供一个全参的构造方法

SpringBoot集成Shiro安全框架

Apache Shiro是Java的一个安全框架。目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Security,可能没有Spring Security做的功能强大,但是在实际工作时可能并不需要那么复杂的东西,所以使用小而简单的Shiro就足够了。对于它俩到底哪个好,这个不必纠结,能更简单的解决项目问题就好了。

Shiro有哪些功能:身份认证/登录,授权,会话管理,加密,Web支持。。。。

记住这么一点,Shiro不会去维护用户、维护权限;这些需要我们自己去设计/提供;通过Realm让开发人员自己注入。

首先引入shiro相关jar包

<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.4.0</version>
</dependency>

然后在src/main/resources下新建一个关于shiro的缓存配置文件ehcache-shiro.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" name="cacheManagerConfigFile">
<defaultCache
          maxElementsInMemory="10000"
          eternal="false"
          timeToIdleSeconds="120"
          timeToLiveSeconds="120"
          overflowToDisk="false"
          diskPersistent="false"
          diskExpiryThreadIntervalSeconds="120"
          memoryStoreEvictionPolicy="LRU"/>
 <cache name="shiro-activeSessionCache"
   eternal="false"
        maxElementsInMemory="10000"
        overflowToDisk="false"
        timeToIdleSeconds="0"
        timeToLiveSeconds="0"
        statistics="true"/>
</ehcache>

然后新建一个Shiro的配置文件在com.majiaxueyuan.config包下ShiroConfig并注解@Configuration,这个配置只不过把最初的spring-shiro.xml形式的配置文件转成了java文件,这个大家自己拿下去看下就明白意思了,内容和xml形式完全一致

@Configuration
public class ShiroConfiguration {
/**
* ShiroFilterFactoryBean 处理拦截资源文件问题。
* 注意:单独一个ShiroFilterFactoryBean配置是或报错的,以为在
* 初始化ShiroFilterFactoryBean的时候需要注入:SecurityManager
* Filter Chain定义说明 1、一个URL可以配置多个Filter,使用逗号分隔 2、当设置多个过滤器时,全部验证通过,才视为通过
* 3、部分过滤器可指定参数,如perms,roles
*/
@Bean
public ShiroFilterFactoryBean shirFilter(org.apache.shiro.mgt.SecurityManager securityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

// 必须设置 SecurityManager
shiroFilterFactoryBean.setSecurityManager(securityManager);

// 拦截器.
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
//配置静态资源允许访问
filterChainDefinitionMap.put("/js/**","anon");
filterChainDefinitionMap.put("/css/**","anon");
filterChainDefinitionMap.put("/index","anon");
// <!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
filterChainDefinitionMap.put("/**", "authc");
// 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面
shiroFilterFactoryBean.setLoginUrl("/login");
// 未授权界面;
shiroFilterFactoryBean.setUnauthorizedUrl("/403");
Map<String, Filter> filters=new HashMap<String,Filter>();
shiroFilterFactoryBean.setFilters(filters);
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return shiroFilterFactoryBean;
}

@Bean
public EhCacheManager getEhCacheManager() {
EhCacheManager em = new EhCacheManager();
em.setCacheManagerConfigFile("classpath:ehcache-shiro.xml");
return em;
}
           // 开启Controller中的shiro注解
@Bean
public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
DefaultAdvisorAutoProxyCreator daap = new DefaultAdvisorAutoProxyCreator();
daap.setProxyTargetClass(true);
return daap;
}
/**
* 配置org.apache.shiro.web.session.mgt.DefaultWebSessionManager
* @return
*/
@Bean
public DefaultWebSessionManager getDefaultWebSessionManager(){
DefaultWebSessionManager defaultWebSessionManager=new DefaultWebSessionManager();
defaultWebSessionManager.setSessionDAO(getMemorySessionDAO());
defaultWebSessionManager.setGlobalSessionTimeout(4200000);
defaultWebSessionManager.setSessionValidationSchedulerEnabled(true);
defaultWebSessionManager.setSessionIdCookieEnabled(true);
defaultWebSessionManager.setSessionIdCookie(getSimpleCookie());
return defaultWebSessionManager;
}
/**
* 配置org.apache.shiro.session.mgt.eis.MemorySessionDAO
* @return
*/
@Bean
public MemorySessionDAO getMemorySessionDAO(){
MemorySessionDAO memorySessionDAO=new MemorySessionDAO();
memorySessionDAO.setSessionIdGenerator(javaUuidSessionIdGenerator());
return memorySessionDAO;
}
@Bean
public JavaUuidSessionIdGenerator javaUuidSessionIdGenerator(){
return new JavaUuidSessionIdGenerator();
}
/**
* session自定义cookie名
* @return
*/
@Bean
public SimpleCookie getSimpleCookie(){
SimpleCookie simpleCookie=new SimpleCookie();
simpleCookie.setName("security.session.id");
simpleCookie.setPath("/");
return simpleCookie;
}
@Bean
public LifecycleBeanPostProcessor getLifecycleBeanPostProcessor(){
return new LifecycleBeanPostProcessor();
}
           @Bean(name = "securityManager")
           public DefaultWebSecurityManager getDefaultWebSecurityManager(UserRealm userRealm) {
                       DefaultWebSecurityManager dwsm = new DefaultWebSecurityManager();
                       dwsm.setRealm(userRealm);
                        //  <!-- 用户授权/认证信息Cache, 采用EhCache 缓存 -->
                       dwsm.setCacheManager(getEhCacheManager());
                       dwsm.setSessionManager(getDefaultWebSessionManager());
                       return dwsm;
             }
             //登录的时候必须走这里
             //这里开始会报错,在下面创建相应的类。
@Bean
public UserRealm userRealm(EhCacheManager cacheManager) {
UserRealm userRealm = new UserRealm();
userRealm.setCacheManager(cacheManager);
return userRealm;
}
/**
* 开启shrio注解支持
* @param userRealm
* @return
*/
@Bean
public AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor(UserRealm userRealm){
AuthorizationAttributeSourceAdvisor aasa=new AuthorizationAttributeSourceAdvisor();
aasa.setSecurityManager(getDefaultWebSecurityManager(userRealm));
return aasa;
}
}

然后在com.realm写一个UserRealm做登录权限控制:

//在上面的配置文件中有这个配置,需要引入这个类

public class UserRealm extends AuthorizingRealm {
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
// 这里做权限控制
return null;
}
           @Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
// 这里做登录控制
return null;
}
}

我们这里就在UserContrller里面去使用我们的shiro进行登录权限控制

UsernamePasswordToken token = new UsernamePasswordToken(username, password);
Security sccurity = SecurityUtils.getSubject();
sccurity.login(token);

这里如果不抛出异常证明登录成功。抛出异常则证明账号或者密码错误。

这里,基本上shiro的集成使用就到这里了,关于shiro的基础不是我们要用到了,我们这里只是着重的要集成shiro到我们的SpringBooot里面去。

在下一篇详细写shiro的登录控制、权限控制。

SpringBoot学习笔记(五):SpringBoot集成lombok工具、SpringBoot集成Shiro安全框架的更多相关文章

  1. SpringBoot学习笔记五之管理员后台维护

    注:图片如果损坏,点击文章链接:https://www.toutiao.com/i6803544440112677379/ 首先完成分页 引入PageHelper(之前已经添加过了) 在spring- ...

  2. SpringBoot学习笔记(十一:使用MongoDB存储文件 )

    @ 目录 一.MongoDB存储文件 1.MongoDB存储小文件 2.MongoDB存储大文件 2.1.GridFS存储原理 2.2.GridFS使用 2.2.1.使用shell命令 2.2.2.使 ...

  3. Springboot学习笔记(六)-配置化注入

    前言 前面写过一个Springboot学习笔记(一)-线程池的简化及使用,发现有个缺陷,打个比方,我这个线程池写在一个公用服务中,各项参数都定死了,现在有两个服务要调用它,一个服务的线程数通常很多,而 ...

  4. SpringBoot学习笔记(2):引入Spring Security

    SpringBoot学习笔记(2):用Spring Security来保护你的应用 快速开始 本指南将引导您完成使用受Spring Security保护的资源创建简单Web应用程序的过程. 参考资料: ...

  5. SpringBoot学习笔记(7):Druid使用心得

    SpringBoot学习笔记(7):Druid使用心得 快速开始 添加依赖 <dependency> <groupId>com.alibaba</groupId> ...

  6. SpringBoot学习笔记:Swagger实现文档管理

    SpringBoot学习笔记:Swagger实现文档管理 Swagger Swagger是一个规范且完整的框架,用于生成.描述.调用和可视化RESTful风格的Web服务.Swagger的目标是对RE ...

  7. SpringBoot学习笔记:Redis缓存

    SpringBoot学习笔记:Redis缓存 关于Redis Redis是一个使用ANSI C语言编写的免费开源.支持网络.可基于内存亦可以持久化的日志型.键值数据库.其支持多种存储类型,包括Stri ...

  8. SpringBoot学习笔记:读取配置文件

    SpringBoot学习笔记:读取配置文件 配置文件 在以往的项目中,我们主要通过XML文件进行框架配置,业务的相关配置会放在属性文件中,然后通过一个属性读取的工具类来读取配置信息.在SpringBo ...

  9. SpringBoot学习笔记

    SpringBoot个人感觉比SpringMVC还要好用的一个框架,很多注解配置可以非常灵活的在代码中运用起来: springBoot学习笔记: .一.aop: 新建一个类HttpAspect,类上添 ...

随机推荐

  1. 使用python和tushare股票交易日历数据,判断节假日周末休市

    接口:trade_cal 描述:获取各大交易所交易日历数据,默认提取的是上交所 注:tushare模块下载和安装教程,请查阅我之前的文章 输入参数 名称       |       类型        ...

  2. linux使用nmon监控、分析系统性能

    linux使用nmon监控.分析系统性能   一.概述 nmon是一种在AIX与各种Linux操作系统上广泛使用的监控与分析工具,相对于其它一些系统资源监控工具来说,nmon所记录的信息是比较全面的, ...

  3. 面试系列32 集群部署时的分布式session如何实现

    session是啥?浏览器有个cookie,在一段时间内这个cookie都存在,然后每次发请求过来都带上一个特殊的jsessionid cookie,就根据这个东西,在服务端可以维护一个对应的sess ...

  4. D3.js(v3)+react 制作 一个带坐标与比例尺的柱形图 (V3版本)

    现在用D3.js + react做一个带坐标轴和比例尺的柱形图.我已经尽力把代码全部注释上了,最后我也会把完整柱形图代码奉上.如果还有疑惑的,可以去翻看一下我之前介绍的方法,以下方法都有介绍到. 还有 ...

  5. JS对象 返回/设置年份方法 get/setFullYear() 返回/设置年份,用四位数表示。.顺序格式依次为:星期、月、日、年、时、分、秒、时区。(火狐浏览器)

    返回/设置年份方法 get/setFullYear() 返回/设置年份,用四位数表示. var mydate=new Date();//当前时间2014年3月6日 document.write(myd ...

  6. 【笔记篇】斜率优化dp(三) APIO2010特别行动队

    旁听了一波给舒老师和学弟的pkuwc面试讲座... 这里有一段隐身的吐槽, 想看的请自己想办法观看. 不想看的跳过这一段看似空白的东西就好了... 刚开始ATP学姐给我们讲了自己面试的时候的事情..描 ...

  7. C++: string<-->char

    1. char*.char[] 与 std::string 之间的区别: char*是一个指向字符的指针,是一个内置类型.可以指向一个字符,也可以表示字符数组的首地址(首字符的地址).我们更多的时候是 ...

  8. delphi 流程单打印

    1.添加声明 f_count1: double; 2.得到拆分页数量 // Modified by 884 2018-04-20 14:50:18 AM0057 with aqTpCount do b ...

  9. yii2中使用定义在 params.php文件中的配置

    yii2 使用 配置文件中在 params 的配置, 可以用 Yii::$app->params['key1']形式访问 参考 yii can't access Yii::$app->pa ...

  10. 记一次数据丢失(电脑硬盘closed to down)的经历

    早上-高高兴兴上班去. 到了公司,突然发现出现windows.logo一两秒的时候会蓝屏,surprise. 百度了一下代码,ok修改硬盘格式,从ACHI到IDE 进入Bios,嗯?感觉界面变了,咋回 ...