-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
711ad33
commit 0b9ea06
Showing
5 changed files
with
134 additions
and
56 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/main/java/com/uetty/sample/springboot/entity/Role.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.uetty.sample.springboot.entity; | ||
|
||
import org.springframework.security.core.GrantedAuthority; | ||
|
||
public class Role implements GrantedAuthority { | ||
|
||
String name; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String getAuthority() { | ||
return getName(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/main/java/com/uetty/sample/springboot/security/AuthenticationProviderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.uetty.sample.springboot.security; | ||
|
||
import com.uetty.sample.springboot.dao.UserDao; | ||
import com.uetty.sample.springboot.entity.User; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.authentication.BadCredentialsException; | ||
import org.springframework.security.authentication.InternalAuthenticationServiceException; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Date; | ||
import java.util.Objects; | ||
|
||
@Component | ||
public class AuthenticationProviderImpl extends AbstractUserDetailsAuthenticationProvider { | ||
|
||
// 限制登录频率 | ||
private static final int MAX_FAILED_TIMES = 5; | ||
private static final long FAILED_INTERVAL = 120_000L; | ||
|
||
@Autowired | ||
UserDao userDao; | ||
|
||
@Override | ||
protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException { | ||
// 校验密码逻辑 | ||
String password = authentication.getCredentials().toString(); | ||
User user = (User) userDetails; | ||
Integer loginFailedTimes = user.getLoginFailedTimes(); | ||
loginFailedTimes = loginFailedTimes == null ? 0 : loginFailedTimes; | ||
long lastLoginTime = user.getLastLoginTime() != null ? user.getLastLoginTime().getTime() : 0L; | ||
Date date = new Date(); | ||
// 限制登录失败频率 | ||
if (loginFailedTimes >= MAX_FAILED_TIMES | ||
&& date.getTime() - lastLoginTime < FAILED_INTERVAL) { | ||
throw new BadCredentialsException("high login frequency"); // 登录频率过高 | ||
} | ||
|
||
boolean passwordValid = Objects.equals(password, user.getPassword()); | ||
// 更新登录成功和失败信息 | ||
user.setLoginFailedTimes(passwordValid ? 0 : loginFailedTimes + 1); | ||
user.setLastLoginTime(date); | ||
user.setUpdateTime(date); | ||
userDao.update(user); | ||
if (!passwordValid) { | ||
throw new BadCredentialsException("login failed"); | ||
} | ||
} | ||
|
||
@Override | ||
protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException { | ||
try { | ||
User user = userDao.getByUsername(username); | ||
if (user == null) { | ||
throw new UsernameNotFoundException("username[" + username + "] not found"); | ||
} | ||
return user; | ||
} catch (UsernameNotFoundException ne) { | ||
throw ne; | ||
} catch (Exception e) { | ||
throw new InternalAuthenticationServiceException(e.getMessage(), e); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 0 additions & 39 deletions
39
src/main/java/com/uetty/sample/springboot/security/UserDetailsServiceImpl.java
This file was deleted.
Oops, something went wrong.