2012-02-29 55 views
10

私は、Java Webアプリケーションを保護するためにHTTP Basic Authでspring-securityを使用しています。私のWebアプリケーションでは、別のサービスに対してさらに認証するために、現在のユーザーのユーザー名とパスワードを取得する必要があります。私は、パスワードではなく、ユーザー名を取得することができます。現在のユーザーのパスワードをspring-securityから取得します

SecurityContextHolder.getContext().getAuthentication()を使用してこの情報にアクセスしようとしましたが、ここで示唆したようにHow can I get plaintext password from spring-security?ですが、パスワードはnullとして返されます。

パスワードを取得するにはどうすればよいですか?

ありがとうございました。

これは私のApplicationContext-のsecurity.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:sec="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/security 
     http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 

<sec:http authentication-manager-ref='authenticationManager'> 
    <sec:intercept-url pattern="/spring/**" access="ROLE_USER" /> 
    <sec:http-basic /> 
</sec:http> 

<bean id="basicAuthenticationFilter" 
    class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter"> 
    <property name="authenticationManager" ref="authenticationManager" /> 
    <property name="authenticationEntryPoint" ref="authenticationEntryPoint" /> 
</bean> 

<bean id="authenticationEntryPoint" 
    class="org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint"> 
    <property name="realmName" value="Announcements Rest Realm" /> 
</bean> 

<bean id="authenticationManager" 
    class="org.springframework.security.authentication.ProviderManager"> 
    <property name="providers"> 
     <list> 
      <ref local="authProvider" /> 
     </list> 
    </property> 
</bean> 

<bean id="authProvider" 
    class="org.springframework.security.authentication.dao.DaoAuthenticationProvider"> 
    <property name="userDetailsService" ref="userDetailsService" /> 
</bean> 

<bean id="filterChainProxy" class="org.springframework.security.web.FilterChainProxy"> 
    <constructor-arg> 
     <list> 
      <sec:filter-chain pattern="/spring/**" filters="basicAuthenticationFilter" /> 
     </list> 
    </constructor-arg> 
</bean> 

<sec:user-service id="userDetailsService"> 
    <sec:user name="admin" password="**" authorities="ROLE_USER, ROLE_ADMIN" /> 
    <sec:user name="user" password="**" authorities="ROLE_USER" /> 
</sec:user-service> 

答えて

0

では、スイッチのユーザー/として実行/私を覚えて使用していますか?そのような場合、passwordはnullです(上記のスレッドで説明したように)。また、基本フォーム認証を使用する必要があります(j_spring_security_checkへのリクエストを投稿してください)。

+0

それらはデフォルトで有効になっている場合を除き、私は(私を覚えて実行したり、私の知る限り承知しているように、ユーザーを切り替えて使用していませんよ?私は自動設定を使用していません)。また、WebアプリケーションがWebサービスであるため、フォーム認証を使用できません。 – ssloan

+0

大丈夫ですので、ユーザーに認証が成功したことをSIに知らせるにはどうすればよいですか? SecurityContextHolder.getContext()。setAuthentication(...)を使用する場合は、どのような種類のトークンを渡しますか? –

+0

私はsetAuthentication()を直接呼び出すのではありません。設定したBeanの1つで起きている必要があります。私は自分の投稿をapplicationContext-security.xmlで更新してくれます。 – ssloan

0

セッションオブジェクトにパスワードを含む現在のユーザーの詳細をすべて設定している場合は、そのセッションオブジェクトからパスワードを取得できます。

+0

セッション内の唯一のものは、getAuthenticationから返されるものと同じオブジェクトで、getcredentials()メソッドはnullを返します。 – ssloan

+1

これはSpring 3.1以降ではデフォルトでは動作しません。認証が成功すると、認証情報は消去されます。http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#core-services-erasing-credentials –

22

私はそれを理解しました。

は私が認証-manager要素を使用するために私の認証マネージャの設定を変更し、そこに属性を追加しました:私は、パスワードを取得するために私のコントローラクラスでSecurityContextHolder.getContext().getAuthentication().getCredentials()を使用することができます

<sec:authentication-manager alias="authenticationManager" erase-credentials="false"> 
    <sec:authentication-provider ref="authProvider" />  
</sec:authentication-manager> 

。あなたの助けのための

おかげPiotrekデ

+0

ニース検索をご覧ください。 Did not know 'erase-credentials =" false "' – cowls

+0

これは私が必要としていたものですが、私はセキュリティの問題について少し心配しています。しかし私はそれらの問題を避ける他の賢明な方法を見ていない。 –

+0

LDAPログインにもうまく機能します。 – romhail

0

良い方法は、あなたの「userDetailsS​​ervice」を実装することですが。認証を行う際にUserDetailsを挿入する方法を制御できます。私は、フォームベースの認証を実装して、私の自己定義されたユーザーサービスを書いた、

@Service("userService") 
public class UserServiceImpl extends GenericService<User> implements UserDetailsService,UserService{ 



    @Override 
     @Transactional 
     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException { 
       // Declare a null Spring User 
      UserDetails userDetails = null; 



      // Search database for a user that matches the specified username 
      // You can provide a custom DAO to access your persistence layer 
      // Or use JDBC to access your database 
      // DbUser is our custom domain user. This is not the same as Spring's User 
      User dbUser=this.findUserByUserName(username); 

      if (dbUser == null) { 
       throw new UsernameNotFoundException("HiMVC Security:: Error in retrieving user(username=" + username + ")"); 
      } 



      userDetails = new org.springframework.security.core.userdetails.User(
        dbUser.getUserName(), 
        dbUser.getPassword(),//here you can put a clear text password 
        true, 
        true, 
        true, 
        true, 
        loadUserAuthorities(username)); 


      return userDetails; 
     } 
関連する問題