Profil de HongliangHongliang's notebookPhotosBlogLivre d'orPlus Outils Aide

Blog


20 janvier

Spring Security (1) - Config

A Minimal <http> Configuration

All you need to enable web security to begin with is

  <http auto-config='true'>

    <intercept-url pattern="/**" access="ROLE_USER" />

  </http>

 

You can use multiple <intercept-url> elements to define different access requirements for different sets of URLs, but they will be evaluated in the order listed and the first match will be used. So you must put the most specific matches at the top.

 

To add some users, you can define a set of test data directly in the namespace:

  <authentication-provider>

    <user-service>

      <user name="jimi" password="jimispassword" authorities="ROLE_USER, ROLE_ADMIN" />

      <user name="bob" password="bobspassword" authorities="ROLE_USER" />

    </user-service>

  </authentication-provider>

 

 

auto-config Requires a UserDetailsService

An error can occur when using auto-config without a UserDetailsService in your application context (for example, if you are using LDAP authentication). This is because remember-me is automatically enabled when auto-config="true" and it requires an authentication mechanism which uses a UserDetailsService to function (see the Remember-me chapter for more details). If you have an error caused by a missing UserDetailsService then try removing the auto-config setting (and any remember-me setting you might have).  For example,

  <authentication-provider user-service-ref='myUserDetailsService'/>

 

<beans:bean id="myUserDetailsService"

            class="org.springframework.security.userdetails.jdbc.JdbcDaoImpl">

              <beans:property name="dataSource" ref="dataSource"/>

  </beans:bean>

 

Concurrent Session Control

If you wish to place constraints on a single user's ability to log in to your application, Spring Security supports this out of the box with the following simple additions. First you need to add the following listener to your web.xml file to keep Spring Security updated about session lifecycle events:

<listener>

<listener-class>

     org.springframework.security.ui.session.HttpSessionEventPublisher

</listener-class>

</listener>

Then add the following line to your application context:    

  <http>

    ...

    <concurrent-session-control max-sessions="1" />

  </http>

 

 

Adding in Your Own Filters

Each Spring Security filter implements the Spring Ordered interface and the filters created by the namespace are sorted during initialization. The standard Spring Security filters each have an alias in the namespace. The filters, aliases and namespace elements/attributes which create the filters can be found at “Standard Filter Aliases and Ordering”.

 

Method Security

Adding an annotation to a method (on a class or interface) would then limit the access to that method accordingly. Spring Security's native annotation support defines a set of attributes for the method. These will be passed to the AccessDecisionManager for it to make the actual decision.

    @Secured("IS_AUTHENTICATED_ANONYMOUSLY")

    public Account readAccount(Long id);

 

The following example protects all methods on beans declared in the application context whose classes are in the com.mycompany package and whose class names end in "Service". Only users with the ROLE_USER role will be able to invoke these methods. As with URL matching, the most specific matches must come first in the list of pointcuts, as the first matching expression will be used

<global-method-security>

  <protect-pointcut expression="execution(*com.mycompany.*Service.*(..))" access="ROLE_USER"/>

</global-method-security>

 

Or, you can

<bean:bean id="target" class="com.mycompany.myapp.MyBean">

    <intercept-methods>

        <protect method="set*" access="ROLE_ADMIN" />

        <protect method="get*" access="ROLE_ADMIN,ROLE_USER" />

        <protect method="doSomething" access="ROLE_USER" />

    </intercept-methods>

</bean:bean> 

Commentaires

Veuillez patienter...
Le commentaire entré est trop long. Raccourcissez-le.
Vous n'avez rien entré. Réessayez.
Il est actuellement impossible d'ajouter votre commentaire. Réessayez plus tard.
Pour ajouter un commentaire, tu dois avoir l'autorisation de tes parents. Demander l'autorisation
Tes parents ont désactivé les commentaires.
Il est actuellement impossible de supprimer votre commentaire. Réessayez plus tard.
Vous avez dépassé le nombre maximal de commentaires qu'il est possible d'envoyer le même jour. Réessayez dans 24 heures.
Votre compte a pu laisser les commentaires désactivés parce que nos systèmes indiquent que vous risquez d'arroser d'autres utilisateurs de messages. Si vous pensez que votre compte a été désactivé par erreur, contactez l'assistance en ligne de Windows Live.
Effectuez la vérification de sécurité ci-dessous pour finaliser l'envoi de votre commentaire.
Les caractères entrés pour la vérification de sécurité doivent correspondre à ceux de l'image ou du fichier audio.

Pour ajouter un commentaire, connectez-vous avec votre identifiant Windows Live ID (si vous utilisez Messenger ou Xbox LIVE, vous avez un identifiant Windows Live ID). Connectez-vous


Vous n'avez pas d'identifiant Windows Live ID ? Inscrivez-vous

Rétroliens

L'URL de rétrolien de ce billet est :
http://mybiblebook.spaces.live.com/blog/cns!8D4E97688DE1E193!441.trak
Blogs Web qui font référence à ce billet
  • Aucune