DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Develop a Secure CRUD Application Using Angular and Spring Boot
  • How to Implement Two-Factor Authentication in a Spring Boot OAuth Server? Part 1: Configuration
  • Leveraging Salesforce Using Spring Boot
  • How to Use Java to Build Single Sign-on

Trending

  • Spring WebFlux Retries
  • GitHub Action Recipes: Building and Pushing Docker Images to a Container Registry
  • Unleashing the Power of Microservices With Spring Cloud
  • Deploy a Session Recording Solution Using Ansible and Audit Your Bastion Host
  1. DZone
  2. Coding
  3. Frameworks
  4. Secure your Spring Boot Web App in 5 Minutes with pac4j

Secure your Spring Boot Web App in 5 Minutes with pac4j

pac4j is a full security library, easy and powerful, which supports authentication and authorization, but also application logout and advanced features like CSRF protection.

Jérôme Leleu user avatar by
Jérôme Leleu
·
Nov. 04, 15 · News
Like (5)
Save
Tweet
Share
13.39K Views

Join the DZone community and get the full member experience.

Join For Free

I'm proud to announce the release of spring-webmvc-pac4j v1.0 (https://github.com/pac4j/spring-webmvc-pac4j) based on pac4j v1.8 (https://github.com/pac4j/pac4j) for any Spring MVC / Boot web application. It's a full security library, easy and powerful, which supports authentication and authorization, but also application logout and advanced features like CSRF protection.

It supports most authentication mechanisms: OAuth (Facebook, Twitter, Google, Yahoo...), CAS, HTTP (form, basic auth...), OpenID, SAML, Google App Engine, OpenID Connect, JWT, LDAP, RDBMS, MongoDB and Stormpath and authorization checks (role / permission, CSRF token...)

In four easy steps, secure your webapp:

1) add the dependency on the library (spring-webmvc-pac4j) and on the required authentication mechanisms (the pac4j-oauth library for Facebook for example)

2) define the authentication mechanisms (clients) and authorizers (to check authorizations). For example: Facebook authentication and ROLE_ADMIN

@Configuration
public class Pac4jConfig {

    @Bean
    public Config config() {
        FacebookClient facebookClient = new FacebookClient("fbId", "fbSecret");
        Config config = new Config("http://localhost:8080/callback", facebookClient);
        config.addAuthorizer("admin", new RequireAnyRoleAuthorizer("ROLE_ADMIN"));
        return config;
    }
}

3) Define the callback controller on the /callback url (by scanning the appropriate package):


@ComponentScan(basePackages = "org.pac4j.springframework.web")


4) Secure the /facebook/* url to require the user to be authenticated and perform a Facebook authentication if he is not:

@Configuration
public class SecurityConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private Config config;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry
          .addInterceptor(new RequiresAuthenticationInterceptor(config, "FacebookClient"))
          .addPathPatterns("/facebook/*");
    }
}

or in addition, requires the user to have the ROLE_ADMIN:

@Configuration
public class SecurityConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private Config config;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry
          .addInterceptor(new RequiresAuthenticationInterceptor(config, "FacebookClient", "admin"))
          .addPathPatterns("/facebook/*");
    }
}

Read the documentation: https://github.com/pac4j/spring-webmvc-pac4j and the demo: https://github.com/pac4j/spring-webmvc-pac4j-boot-demo

Spring Framework security Spring Boot app authentication

Opinions expressed by DZone contributors are their own.

Related

  • Develop a Secure CRUD Application Using Angular and Spring Boot
  • How to Implement Two-Factor Authentication in a Spring Boot OAuth Server? Part 1: Configuration
  • Leveraging Salesforce Using Spring Boot
  • How to Use Java to Build Single Sign-on

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: