アプリケーションでSpringMVCを使用しており、ログインはSpringセキュリティによって認証されています。UserServiceImpl.java
クラスには次の2つのメソッドがあります。publicUserDetailsloadUserByUsername(String userName)throws UsernameNotFoundException、DataAccessException {
ApplicationTO applicationTO = null;
try
{
applicationTO = applicationService.getApplicationTO(adminDomainName);
}
catch (ApplicationPropertyException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
UserTO userTO = getUserTO(applicationTO.getApplicationId(), userName);
if (userTO == null)
{
throw new UsernameNotFoundException("user not found");
}
httpSession.setAttribute("userTO", userTO);
return buildUserFromUserEntity(userTO);
}
User buildUserFromUserEntity(UserTO userTO)
{
String username = userTO.getUsername();
String password = userTO.getPassword();
int userId = userTO.getUserId();
int applicationId = userTO.getApplicationId();
boolean enabled = userTO.isEnabled();
boolean accountNonExpired = true;
boolean credentialsNonExpired = true;
boolean accountNonLocked = true;
User user = new User(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, getAuthority(applicationId, userId));
return user;
}
私はSpringに比較的慣れていないので、Springのセキュリティ部分についてはあまりよくわかりません。私のspring-security.xml
ファイルには、次のものがあります。
<form-login login-page="/login" default-target-url="/module/user-home/welcome"
authentication-failure-url="/login?error" username-parameter="username"
password-parameter="password" />
<logout logout-success-url="/login?logout" />
<beans:bean id="daoAuthenticationProvider"
class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<beans:property name="userDetailsService" ref="userDetailsService"/>
</beans:bean>
<beans:bean id="authenticationManager"
class="org.springframework.security.authentication.ProviderManager">
<beans:property name="providers">
<beans:list>
<beans:ref local="daoAuthenticationProvider" />
</beans:list>
</beans:property>
</beans:bean>
そして、私のログインフォームには次のようにアクションが設定されています。
<form id="loginForm" class="form-horizontal" role="form" name='loginForm' action="${rc.getContextPath()}/j_spring_security_check" method='POST'>
現在password
、loadUserByUsername
メソッド内で、またはUserServiceImpl.java
クラスに新しいメソッドを追加することにより、ユーザーがログインフォームに入力した値を取得しようとしています。
パスワードを保存する前に、以下を使用してパスワードを暗号化します。Javaを使用してパスワードを暗号化および復号化するために使用するAPIとアルゴリズム
そのため、ログイン時に、Spring Securityはユーザーが入力したパスワードをデータベースからの暗号化されたパスワードと比較し、ログインは失敗します。ただし、上記のリンクで提案されている実装に従って、パスワードと暗号化されたパスワードを比較して、それらが同じであるかどうかを確認する方法があります。これは、ユーザーが入力したパスワードにアクセスできる場合にのみ可能です。これが、ユーザーが入力したパスワードを取得しようとしている理由です。
MangEngkusの回答で提案されたように、独自のカスタムAuthenticationProviderを実装できますが、説明に基づいて、それを行う必要はないと思います。
独自のパスワードハッシュメカニズムをspring-securityに実装する必要はありません。Spring自体からBCryptPasswordEncoderを定義する必要があります。
デフォルトのものを使用するには、次のいずれかの方法で行います。
<authentication-manager>
<authentication-provider>
<password-encoder hash="bcrypt" />
</authentication-provider>
</authentication-manager>
または、独自のBeanを作成して、デフォルトのプロバイダーに提供します。
<authentication-manager>
<authentication-provider>
<password-encoder ref="encoder" />
</authentication-provider>
</authentication-manager>
<beans:bean id="encoder"
class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
<beans:constructor-arg name="strength" value="15" />
</beans:bean>
しかし、あなたにとって、これは方法です::)
<beans:bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<beans:property name="userDetailsService" ref="userDetailsService"/>
<beans:property name="passwordEncoder" ref="encoder" />
</beans:bean>
<bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加