Giter Site home page Giter Site logo

spring-security-javaconfig's Introduction

This project has moved. The Spring Security Java configuration can now be found in spring-security-config-3.2.0.RELEASE+ as part of the Spring Security distribution. The Spring Security OAuth sample code can be found at https://github.com/spring-projects/spring-security-oauth-javaconfig and will eventually be merged into Spring Security OAuth.

spring-security-javaconfig's People

Contributors

aspan avatar rwinch avatar spring-operator avatar tekul avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

spring-security-javaconfig's Issues

Javadoc

Once the APIs become a bit more stable, we need to go through and and Java Doc

custom RememberMeService to support custom PersistentLogin

My business application stack is described in issue #50.

There I also have a business model called User.
The old xml spring security configuration supported persistent logins as remember-me service. The default persistent logins service was based on a database table having the
columns: username, token, series, expiredate

This database table did not need the business application and database model demands.
One demand is to use as less database space as possible and directly connecting
the persistent_logins table (or model) to the user model. Thus replace the "username" column by a foreign key column "id_user" (incl. constraint).

These demands involved following implementations:

  • PersistentLogin (model)
  • PersistentLoginRepository (JpaRepository - spring data jpa)
  • UserIdTokenRepository implements PersistentTokenRepository (process data with repository)
  • TokenInvalidateLogoutHandler implements LogoutSuccessHandler (invalidating/deleting on logout)

Ok, now to the SecurityConfig class. The old xml config supported:

<http ...>
    [...]
    <logout invalidate-session="true"
                        success-handler-ref="tokenInvalidateLogoutHandler"/>
    <remember-me services-ref="rememberMeServices"
                 key="933500A9-1D54-4B7B-BC0A-3CE2749250A7" />

The used beans are defined as follows:

@Bean
public LogoutSuccessHandler tokenInvalidateLogoutHandler() {
    return new TokenInvalidateLogoutHandler();
}

@Bean
public PersistentTokenBasedRememberMeServices rememberMeServices() {
    String key = "933500A9-1D54-4B7B-BC0A-3CE2749250A7";
    PersistentTokenBasedRememberMeServices rememberMeServices = 
            new PersistentTokenBasedRememberMeServices(
                    key, passwordUserDetailsService(), tokenRepository());
    rememberMeServices.setAlwaysRemember(true);
    return rememberMeServices;
}

@Bean
public PersistentTokenRepository tokenRepository() {
    return new UserIdTokenRepository();
}

As far as I have noticed, it is currently not possible to set this service.
Including the results of #50 my current http config is as follows:

protected void configure(HttpConfiguration http) throws Exception {
    http
        .rememberMe()
            .and()
        .formLogin()
            .usernameParameter("username")
            .passwordParameter("password")
            .loginPage("/login")
            .failureUrl("/login/error")
            .loginProcessingUrl("/login")
            .defaultSuccessUrl("/login/success", true)
            .and()
        .logout()
            .invalidateHttpSession(true)
            .logoutSuccessHandler(tokenInvalidateLogoutHandler())
            .logoutUrl("/logout")
            .and()
        .sessionManagement()
            .maximumSessions(2)
            .exceptionIfMaximumExceeded(true)
            .expiredUrl("/login");
}

I also read the source code of RememberMeConfigurator.java.
It looks like this "issue" is just a "want to have" to support custom persistent logins:

public RememberMeConfigurator remembeMeServices(RememberMeServices rememberMeServices) {
    this.rememberMeServices = rememberMeServices;
    return this;
}

I think the key should stay random.

Thanks in advance.

"No matching bean ... found" while initializing custom UserDetailsService and AuthenticationProvider

In my business application I am using
the following application stack:

  • MySQL <> Hibernate <> Spring Data JPA (using JpaRepository interfaces)
  • Spring Framework (3.2.2) <> Apache Tiles + Thymeleaf

Before trying the new javaconfig implementations,
I have a working business application (as follows) using spring-security.xml.

Furthermore based on the entities / models from database (User, Role, Auth)
and repositories (JpaRepository from spring-data-jpa) only as interfaces,
I have implemented a custom UserDetailsService and AuthenticationProvider,
in which the repositories of the User model is getting @Inject-ed (and some more).

Thus for configurating these, I provided them as beans:

@Bean
public AuthenticationProvider usernamePasswordAuthenticationProvider() {
    UsernamePasswordAuthenticationProvider authProv =  new UsernamePasswordAuthenticationProvider();
    return authProv;
}

@Bean(name = "userDetailsService")
public UserDetailsService passwordUserDetailsService() {
    return new PasswordUserDetailsService();
}

Like in some of your examples, I configured these in a similar way:

protected AuthenticationManager authenticationManager(
        AuthenticationBuilder authenticationRegistry) throws Exception {
    return authenticationRegistry
            .add(usernamePasswordAuthenticationProvider())
            .userDetails(passwordUserDetailsService()).and()
            .build();
}

So far so good, I think the other config is not important at the moment. When I try to start my tomcat server (in eclipse), I noticed in the logs that spring-data-jpa created the required beans, e.g.:

DefaultListableBeanFactory  - Overriding bean definition for bean 'userRepository': replacing [Root bean: class [org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean]; ... 

The next initialized beans are about my beans from java configuration (mvc, view, persistence), e.g.:

AnnotationConfigWebApplicationContext  - Bean 'dataSourceConfig' of type [class ...]

After the latter initialization usually the xml from spring security is read and beans are created. In case of the java config of spring security (the test problem now), it is complaining that it cannot inject the upper mentioned userRepository, though working in xml mode. The complete exception chain is listed here:
http://pastebin.com/KK9wXR46

Do you have any idea what the problem could be ?
I thought, maybe your java config is intitializing too early,
that my userRepository bean could not be found (though working in xml mode).

Look into removing nesting () in SecurityFilterChainSecurityBuilder

The following are difficult to read

        return springSecurityFilterChain
            .apply(formLogin()
                .permitAll());

See if there are ways to remove the nesting of (). Perhaps one option would be:

        return springSecurityFilterChain
            .formLogin()
                .permitAll()
                .and()
            .logout()
                .logoutUrl("/custom/logout")
                .and()
            .apply(new CustomConfigurator())
                .something("here");

Memory Consumption

Ensure all the configuration objects can be garbage collected after configuration completes

Define Intercept Urls with chaining

instead of specifying both arguments in a single method perhaps it could do something like:

builder
  .antUrl("/user/**").hasRole("ADMIN")

builder
  .anyAntUrl("/signup","/about").permitAll()

builder.
  .request(requestMatcher).hasAuthority("ROLE_USER")    

Create FAQ

  • Why don't I see an @Enable annotation for web security?
  • Why don't I see ref attributes in annotations for referring to objects?
  • Why have intercept-url requires-channel and access been separated?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.