SpringSecurityでのみ承認された場所からの認証を許可する
1. 概要
このチュートリアルでは、非常に興味深いセキュリティ機能、つまりユーザーの場所に基づいてユーザーのアカウントを保護することに焦点を当てます。
簡単に言えば、異常な場所や非標準の場所からのログインをブロックし、ユーザーが安全な方法で新しい場所を有効にできるようにします。
これは登録シリーズの一部であり、当然、既存のコードベースの上に構築されます。
2. ユーザーロケーションモデル
まず、UserLocationモデルを見てみましょう。このモデルはユーザーのログイン場所に関する情報を保持しています。 各ユーザーには、アカウントに関連付けられた場所が少なくとも1つあります。
@Entity
public class UserLocation {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String country;
private boolean enabled;
@ManyToOne(targetEntity = User.class, fetch = FetchType.EAGER)
@JoinColumn(nullable = false, name = "user_id")
private User user;
public UserLocation() {
super();
enabled = false;
}
public UserLocation(String country, User user) {
super();
this.country = country;
this.user = user;
enabled = false;
}
...
}
そして、リポジトリに簡単な取得操作を追加します。
public interface UserLocationRepository extends JpaRepository<UserLocation, Long> {
UserLocation findByCountryAndUser(String country, User user);
}
ご了承ください
- 新しいUserLocationはデフォルトで無効になっています
- 各ユーザーには、アカウントに関連付けられた少なくとも1つの場所があります。これは、登録時にアプリケーションにアクセスした最初の場所です。
3. 登録
次に、登録プロセスを変更してデフォルトのユーザーの場所を追加する方法について説明します。
@PostMapping("/user/registration")
public GenericResponse registerUserAccount(@Valid UserDto accountDto,
HttpServletRequest request) {
User registered = userService.registerNewUserAccount(accountDto);
userService.addUserLocation(registered, getClientIP(request));
...
}
サービスの実装では、ユーザーのIPアドレスで国を取得します。
public void addUserLocation(User user, String ip) {
InetAddress ipAddress = InetAddress.getByName(ip);
String country
= databaseReader.country(ipAddress).getCountry().getName();
UserLocation loc = new UserLocation(country, user);
loc.setEnabled(true);
loc = userLocationRepo.save(loc);
}
GeoLite2 データベースを使用して、IPアドレスから国を取得していることに注意してください。 GeoLite2 を使用するには、Mavenの依存関係が必要でした。
<dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>2.15.0</version>
</dependency>
また、単純なBeanを定義する必要があります。
@Bean
public DatabaseReader databaseReader() throws IOException, GeoIp2Exception {
File resource = new File("src/main/resources/GeoLite2-Country.mmdb");
return new DatabaseReader.Builder(resource).build();
}
MaxMindからGeoLite2Countryデータベースをここにロードしました。
4. 安全なログイン
ユーザーのデフォルトの国がわかったので、認証後に簡単なロケーションチェッカーを追加します。
@Autowired
private DifferentLocationChecker differentLocationChecker;
@Bean
public DaoAuthenticationProvider authProvider() {
CustomAuthenticationProvider authProvider = new CustomAuthenticationProvider();
authProvider.setUserDetailsService(userDetailsService);
authProvider.setPasswordEncoder(encoder());
authProvider.setPostAuthenticationChecks(differentLocationChecker);
return authProvider;
}
そして、これが私たちのDifferentLocationCheckerです。
@Component
public class DifferentLocationChecker implements UserDetailsChecker {
@Autowired
private IUserService userService;
@Autowired
private HttpServletRequest request;
@Autowired
private ApplicationEventPublisher eventPublisher;
@Override
public void check(UserDetails userDetails) {
String ip = getClientIP();
NewLocationToken token = userService.isNewLoginLocation(userDetails.getUsername(), ip);
if (token != null) {
String appUrl =
"http://"
+ request.getServerName()
+ ":" + request.getServerPort()
+ request.getContextPath();
eventPublisher.publishEvent(
new OnDifferentLocationLoginEvent(
request.getLocale(), userDetails.getUsername(), ip, token, appUrl));
throw new UnusualLocationException("unusual location");
}
}
private String getClientIP() {
String xfHeader = request.getHeader("X-Forwarded-For");
if (xfHeader == null) {
return request.getRemoteAddr();
}
return xfHeader.split(",")[0];
}
}
s etPostAuthenticationChecks()を使用して、チェックが認証の成功後にのみ実行されるように –ユーザーが正しい資格情報を提供した場合にのみ実行されることに注意してください。
また、カスタムUnusualLocationExceptionは単純なAuthenticationExceptionです。
また、 AuthenticationFailureHandler を変更して、エラーメッセージをカスタマイズする必要があります。
@Override
public void onAuthenticationFailure(...) {
...
else if (exception.getMessage().equalsIgnoreCase("unusual location")) {
errorMessage = messages.getMessage("auth.message.unusual.location", null, locale);
}
}
それでは、 isNewLoginLocation()の実装を詳しく見てみましょう。
@Override
public NewLocationToken isNewLoginLocation(String username, String ip) {
try {
InetAddress ipAddress = InetAddress.getByName(ip);
String country
= databaseReader.country(ipAddress).getCountry().getName();
User user = repository.findByEmail(username);
UserLocation loc = userLocationRepo.findByCountryAndUser(country, user);
if ((loc == null) || !loc.isEnabled()) {
return createNewLocationToken(country, user);
}
} catch (Exception e) {
return null;
}
return null;
}
ユーザーが正しい資格情報を提供すると、その場所を確認する方法に注目してください。 場所がすでにそのユーザーアカウントに関連付けられている場合、ユーザーは正常に認証できます。
そうでない場合は、NewLocationTokenと無効なUserLocationを作成して、ユーザーがこの新しい場所を有効にできるようにします。 詳細については、次のセクションで説明します。
private NewLocationToken createNewLocationToken(String country, User user) {
UserLocation loc = new UserLocation(country, user);
loc = userLocationRepo.save(loc);
NewLocationToken token = new NewLocationToken(UUID.randomUUID().toString(), loc);
return newLocationTokenRepository.save(token);
}
最後に、単純な NewLocationToken の実装を示します。これにより、ユーザーは新しい場所を自分のアカウントに関連付けることができます。
@Entity
public class NewLocationToken {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String token;
@OneToOne(targetEntity = UserLocation.class, fetch = FetchType.EAGER)
@JoinColumn(nullable = false, name = "user_location_id")
private UserLocation userLocation;
...
}
5. 別の場所のログインイベント
ユーザーが別の場所からログインするときに、 NewLocationToken を作成し、それを使用してOnDifferentLocationLoginEventをトリガーしました。
public class OnDifferentLocationLoginEvent extends ApplicationEvent {
private Locale locale;
private String username;
private String ip;
private NewLocationToken token;
private String appUrl;
}
DifferentLocationLoginListener は、イベントを次のように処理します。
@Component
public class DifferentLocationLoginListener
implements ApplicationListener<OnDifferentLocationLoginEvent> {
@Autowired
private MessageSource messages;
@Autowired
private JavaMailSender mailSender;
@Autowired
private Environment env;
@Override
public void onApplicationEvent(OnDifferentLocationLoginEvent event) {
String enableLocUri = event.getAppUrl() + "/user/enableNewLoc?token="
+ event.getToken().getToken();
String changePassUri = event.getAppUrl() + "/changePassword.html";
String recipientAddress = event.getUsername();
String subject = "Login attempt from different location";
String message = messages.getMessage("message.differentLocation", new Object[] {
new Date().toString(),
event.getToken().getUserLocation().getCountry(),
event.getIp(), enableLocUri, changePassUri
}, event.getLocale());
SimpleMailMessage email = new SimpleMailMessage();
email.setTo(recipientAddress);
email.setSubject(subject);
email.setText(message);
email.setFrom(env.getProperty("support.email"));
mailSender.send(email);
}
}
ユーザーが別の場所からログインすると、に通知するメールが送信されることに注意してください。
他の誰かが自分のアカウントにログインしようとした場合、もちろん、その人は自分のパスワードを変更します。 認証の試行を認識すると、新しいログイン場所を自分のアカウントに関連付けることができます。
6. 新しいログイン場所を有効にする
最後に、疑わしいアクティビティがユーザーに通知されたので、アプリケーションが新しい場所の有効化をどのように処理するかを見てみましょう。
@RequestMapping(value = "/user/enableNewLoc", method = RequestMethod.GET)
public String enableNewLoc(Locale locale, Model model, @RequestParam("token") String token) {
String loc = userService.isValidNewLocationToken(token);
if (loc != null) {
model.addAttribute(
"message",
messages.getMessage("message.newLoc.enabled", new Object[] { loc }, locale)
);
} else {
model.addAttribute(
"message",
messages.getMessage("message.error", null, locale)
);
}
return "redirect:/login?lang=" + locale.getLanguage();
}
そして、 isValidNewLocationToken()メソッド:
@Override
public String isValidNewLocationToken(String token) {
NewLocationToken locToken = newLocationTokenRepository.findByToken(token);
if (locToken == null) {
return null;
}
UserLocation userLoc = locToken.getUserLocation();
userLoc.setEnabled(true);
userLoc = userLocationRepo.save(userLoc);
newLocationTokenRepository.delete(locToken);
return userLoc.getCountry();
}
簡単に言うと、トークンに関連付けられている UserLocation を有効にしてから、トークンを削除します。
7. 制限事項
記事を終了するには、上記の実装の制限について言及する必要があります。 クライアントIPを決定するために使用した方法:
private final String getClientIP(HttpServletRequest request)
クライアントの正しいIPアドレスを常に返すとは限りません。 Spring Bootアプリケーションがローカルにデプロイされている場合、返されるIPアドレスは(別の方法で構成されていない限り)0.0.0.0です。 このアドレスはMaxMindデータベースに存在しないため、登録とログインはできません。 クライアントのIPアドレスがデータベースに存在しない場合も同じ問題が発生します。
8. 結論
このチュートリアルでは、アプリケーションにセキュリティを追加するための強力な新しいメカニズムに焦点を当てました–
いつものように、完全な実装はGiHubのにあります。