作者 朱兆平

add: 配置文件增加redis 密码认证.

用户实体类增加过期时间字段.
数据库用户表增加过期时间字段.
登录认证增加用户过期校验.
@@ -28,6 +28,7 @@ spring: @@ -28,6 +28,7 @@ spring:
28 # host: 127.0.0.1 28 # host: 127.0.0.1
29 host: 192.168.1.53 29 host: 192.168.1.53
30 port: 6379 30 port: 6379
  31 + password: vmvnv1v2VV.
31 jackson: 32 jackson:
32 serialization: 33 serialization:
33 FAIL_ON_EMPTY_BEANS: false 34 FAIL_ON_EMPTY_BEANS: false
@@ -71,6 +71,8 @@ public class KakoUser implements UserDetails { @@ -71,6 +71,8 @@ public class KakoUser implements UserDetails {
71 71
72 private Boolean online; 72 private Boolean online;
73 73
  74 + private Date expiryDate;
  75 +
74 @JSONField(serialzeFeatures= {SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty}) 76 @JSONField(serialzeFeatures= {SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty})
75 private List<ROLE> roles; 77 private List<ROLE> roles;
76 78
@@ -281,13 +283,31 @@ public class KakoUser implements UserDetails { @@ -281,13 +283,31 @@ public class KakoUser implements UserDetails {
281 this.online = online; 283 this.online = online;
282 } 284 }
283 285
  286 + public Date getExpiryDate() {
  287 + return expiryDate;
  288 + }
  289 +
  290 + public void setExpiryDate(Date expiryDate) {
  291 + this.expiryDate = expiryDate;
  292 + }
  293 +
284 /** 294 /**
285 * 295 *
286 * @return 账户未过期 296 * @return 账户未过期
  297 + * 未过期为true,已过期为false
287 */ 298 */
288 @Override 299 @Override
289 public boolean isAccountNonExpired(){ 300 public boolean isAccountNonExpired(){
  301 + // 无过期时间字段时视为永不过期
  302 + if(expiryDate == null) {
290 return true; 303 return true;
  304 + }else {
  305 + Boolean check = new Date().before(expiryDate);
  306 + return check;
  307 + }
  308 +
  309 + // 当前时间在过期时间之前视为未过期
  310 +
291 } 311 }
292 312
293 /** 313 /**
@@ -7,8 +7,10 @@ import org.apache.commons.logging.Log; @@ -7,8 +7,10 @@ import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory; 7 import org.apache.commons.logging.LogFactory;
8 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.http.HttpStatus; 9 import org.springframework.http.HttpStatus;
  10 +import org.springframework.security.authentication.AccountExpiredException;
10 import org.springframework.security.authentication.BadCredentialsException; 11 import org.springframework.security.authentication.BadCredentialsException;
11 import org.springframework.security.authentication.DisabledException; 12 import org.springframework.security.authentication.DisabledException;
  13 +import org.springframework.security.authentication.LockedException;
12 import org.springframework.security.core.AuthenticationException; 14 import org.springframework.security.core.AuthenticationException;
13 import org.springframework.security.core.userdetails.UsernameNotFoundException; 15 import org.springframework.security.core.userdetails.UsernameNotFoundException;
14 import org.springframework.security.web.DefaultRedirectStrategy; 16 import org.springframework.security.web.DefaultRedirectStrategy;
@@ -69,6 +71,8 @@ public class MyAuthenticationFailHandler extends SimpleUrlAuthenticationFailureH @@ -69,6 +71,8 @@ public class MyAuthenticationFailHandler extends SimpleUrlAuthenticationFailureH
69 response.sendError(HttpStatus.UNAUTHORIZED.value(),"用户不存在或者密码错误"); 71 response.sendError(HttpStatus.UNAUTHORIZED.value(),"用户不存在或者密码错误");
70 } else if (exception instanceof DisabledException) { 72 } else if (exception instanceof DisabledException) {
71 response.sendError(HttpStatus.UNAUTHORIZED.value(),"账户被禁用,登录失败,请联系管理员!"); 73 response.sendError(HttpStatus.UNAUTHORIZED.value(),"账户被禁用,登录失败,请联系管理员!");
  74 + } else if (exception instanceof AccountExpiredException) {
  75 + response.sendError(HttpStatus.UNAUTHORIZED.value(),"账户已过期,登录失败,请联系管理员!");
72 } else { 76 } else {
73 response.sendError(HttpStatus.UNAUTHORIZED.value(),"登录失败"); 77 response.sendError(HttpStatus.UNAUTHORIZED.value(),"登录失败");
74 } 78 }
@@ -11,10 +11,7 @@ import org.apache.shiro.crypto.hash.Hash; @@ -11,10 +11,7 @@ import org.apache.shiro.crypto.hash.Hash;
11 import org.apache.shiro.crypto.hash.SimpleHash; 11 import org.apache.shiro.crypto.hash.SimpleHash;
12 import org.apache.shiro.util.ByteSource; 12 import org.apache.shiro.util.ByteSource;
13 import org.springframework.beans.factory.annotation.Autowired; 13 import org.springframework.beans.factory.annotation.Autowired;
14 -import org.springframework.security.authentication.AuthenticationProvider;  
15 -import org.springframework.security.authentication.BadCredentialsException;  
16 -import org.springframework.security.authentication.DisabledException;  
17 -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 14 +import org.springframework.security.authentication.*;
18 import org.springframework.security.core.Authentication; 15 import org.springframework.security.core.Authentication;
19 import org.springframework.security.core.AuthenticationException; 16 import org.springframework.security.core.AuthenticationException;
20 import org.springframework.security.core.userdetails.UserDetails; 17 import org.springframework.security.core.userdetails.UserDetails;
@@ -63,6 +60,11 @@ public class MyLoginAuthenticationProvider extends CodecSupport implements Authe @@ -63,6 +60,11 @@ public class MyLoginAuthenticationProvider extends CodecSupport implements Authe
63 throw new DisabledException("用户被禁用"); 60 throw new DisabledException("用户被禁用");
64 } 61 }
65 62
  63 + // 过期判定
  64 + if (!userInfo.isAccountNonExpired()){
  65 + throw new AccountExpiredException("用户已过期");
  66 + }
  67 +
66 //取盐规则 68 //取盐规则
67 byte[] salt = PasswordSaltUtils.getSalt16(userInfo.getPassword()); 69 byte[] salt = PasswordSaltUtils.getSalt16(userInfo.getPassword());
68 //真实密码 70 //真实密码
@@ -24,11 +24,12 @@ @@ -24,11 +24,12 @@
24 <result column="remarks" property="remarks" jdbcType="VARCHAR" /> 24 <result column="remarks" property="remarks" jdbcType="VARCHAR" />
25 <result column="del_flag" property="delFlag" jdbcType="CHAR" /> 25 <result column="del_flag" property="delFlag" jdbcType="CHAR" />
26 <result column="online" property="online" jdbcType="BOOLEAN" /> 26 <result column="online" property="online" jdbcType="BOOLEAN" />
  27 + <result column="expiry_date" property="expiryDate" jdbcType="TIMESTAMP" />
27 </resultMap> 28 </resultMap>
28 <sql id="Base_Column_List" > 29 <sql id="Base_Column_List" >
29 id, company_id, office_id, login_name, password, no, name, email, phone, mobile, 30 id, company_id, office_id, login_name, password, no, name, email, phone, mobile,
30 user_type, photo, login_ip, login_date, login_flag, create_by, create_date, update_by, 31 user_type, photo, login_ip, login_date, login_flag, create_by, create_date, update_by,
31 - update_date, remarks, del_flag, online 32 + update_date, remarks, del_flag, online, expiry_date
32 </sql> 33 </sql>
33 <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" > 34 <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
34 select 35 select
@@ -83,7 +84,7 @@ @@ -83,7 +84,7 @@
83 mobile, user_type, photo, 84 mobile, user_type, photo,
84 login_ip, login_date, login_flag, 85 login_ip, login_date, login_flag,
85 create_by, create_date, update_by, 86 create_by, create_date, update_by,
86 - update_date, remarks, del_flag 87 + update_date, remarks, del_flag, expiry_date
87 ) 88 )
88 values (#{id,jdbcType=VARCHAR}, #{companyId,jdbcType=VARCHAR}, #{officeId,jdbcType=VARCHAR}, 89 values (#{id,jdbcType=VARCHAR}, #{companyId,jdbcType=VARCHAR}, #{officeId,jdbcType=VARCHAR},
89 #{loginName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{no,jdbcType=VARCHAR}, 90 #{loginName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{no,jdbcType=VARCHAR},
@@ -91,7 +92,7 @@ @@ -91,7 +92,7 @@
91 #{mobile,jdbcType=VARCHAR}, #{userType,jdbcType=CHAR}, #{photo,jdbcType=VARCHAR}, 92 #{mobile,jdbcType=VARCHAR}, #{userType,jdbcType=CHAR}, #{photo,jdbcType=VARCHAR},
92 #{loginIp,jdbcType=VARCHAR}, #{loginDate,jdbcType=TIMESTAMP}, #{loginFlag,jdbcType=VARCHAR}, 93 #{loginIp,jdbcType=VARCHAR}, #{loginDate,jdbcType=TIMESTAMP}, #{loginFlag,jdbcType=VARCHAR},
93 #{createBy,jdbcType=VARCHAR}, #{createDate,jdbcType=TIMESTAMP}, #{updateBy,jdbcType=VARCHAR}, 94 #{createBy,jdbcType=VARCHAR}, #{createDate,jdbcType=TIMESTAMP}, #{updateBy,jdbcType=VARCHAR},
94 - #{updateDate,jdbcType=TIMESTAMP}, #{remarks,jdbcType=VARCHAR}, #{delFlag,jdbcType=CHAR} 95 + #{updateDate,jdbcType=TIMESTAMP}, #{remarks,jdbcType=VARCHAR}, #{delFlag,jdbcType=CHAR}, #{expiryDate,jdbcType=TIMESTAMP}
95 ) 96 )
96 </insert> 97 </insert>
97 <insert id="insertSelective" parameterType="com.tianbo.warehouse.model.KakoUser" > 98 <insert id="insertSelective" parameterType="com.tianbo.warehouse.model.KakoUser" >
@@ -160,6 +161,9 @@ @@ -160,6 +161,9 @@
160 <if test="delFlag != null" > 161 <if test="delFlag != null" >
161 del_flag, 162 del_flag,
162 </if> 163 </if>
  164 + <if test="expiryDate != null" >
  165 + expiry_date,
  166 + </if>
163 </trim> 167 </trim>
164 <trim prefix="values (" suffix=")" suffixOverrides="," > 168 <trim prefix="values (" suffix=")" suffixOverrides="," >
165 <if test="id != null" > 169 <if test="id != null" >
@@ -225,6 +229,9 @@ @@ -225,6 +229,9 @@
225 <if test="delFlag != null" > 229 <if test="delFlag != null" >
226 #{delFlag,jdbcType=CHAR}, 230 #{delFlag,jdbcType=CHAR},
227 </if> 231 </if>
  232 + <if test="expiryDate != null" >
  233 + #{expiryDate,jdbcType=TIMESTAMP},
  234 + </if>
228 </trim> 235 </trim>
229 </insert> 236 </insert>
230 <update id="updateByPrimaryKeySelective" parameterType="com.tianbo.warehouse.model.KakoUser" > 237 <update id="updateByPrimaryKeySelective" parameterType="com.tianbo.warehouse.model.KakoUser" >
@@ -293,6 +300,9 @@ @@ -293,6 +300,9 @@
293 <if test="online != null" > 300 <if test="online != null" >
294 online = #{online,jdbcType=BOOLEAN}, 301 online = #{online,jdbcType=BOOLEAN},
295 </if> 302 </if>
  303 + <if test="expiryDate != null" >
  304 + expiry_date = #{expiryDate,jdbcType=TIMESTAMP},
  305 + </if>
296 </set> 306 </set>
297 where id = #{id,jdbcType=VARCHAR} 307 where id = #{id,jdbcType=VARCHAR}
298 </update> 308 </update>
@@ -318,7 +328,8 @@ @@ -318,7 +328,8 @@
318 update_date = #{updateDate,jdbcType=TIMESTAMP}, 328 update_date = #{updateDate,jdbcType=TIMESTAMP},
319 remarks = #{remarks,jdbcType=VARCHAR}, 329 remarks = #{remarks,jdbcType=VARCHAR},
320 del_flag = #{delFlag,jdbcType=CHAR}, 330 del_flag = #{delFlag,jdbcType=CHAR},
321 - online = #{online,jdbcType=BOOLEAN} 331 + online = #{online,jdbcType=BOOLEAN},
  332 + expiry_date = #{expiryDate,jdbcType=TIMESTAMP}
322 where id = #{id,jdbcType=VARCHAR} 333 where id = #{id,jdbcType=VARCHAR}
323 </update> 334 </update>
324 335