Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Spring 5.0 Cookbook

You're reading from   Spring 5.0 Cookbook Recipes to build, test, and run Spring applications efficiently

Arrow left icon
Product type Paperback
Published in Sep 2017
Publisher Packt
ISBN-13 9781787128316
Length 670 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Sherwin John C. Tragura Sherwin John C. Tragura
Author Profile Icon Sherwin John C. Tragura
Sherwin John C. Tragura
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Getting Started with Spring 2. Learning Dependency Injection (DI) FREE CHAPTER 3. Implementing MVC Design Patterns 4. Securing Spring MVC Applications 5. Cross-Cutting the MVC 6. Functional Programming 7. Reactive Programming 8. Reactive Web Applications 9. Spring Boot 2.0 10. The Microservices 11. Batch and Message-Driven Processes 12. Other Spring 5 Features 13. Testing Spring 5 Components

Applying Security to MVC methods


From architectural-level authorization, we go down to the access levels of our service and controller methods. This recipe will design a role-based authorization imposed on some essential transactions of the MVC application.

Getting started

We will utilize the same ch04 project, but this time we will focus on role-based authorization of the service and request methods.

How to do it...

  1. Before we apply Spring Security on some service methods, let us open the UserServiceImpl class and add the following authorization: a super-user role to hradmin by adding ROLE_USER to its existing set of authorities; ROLE_ADMIN and ROLE_USER authorities to the "admin" account; and ROLE_USER authorization to the "sjctrags" account:
@Service("userService") 
public class UserServiceImpl implements UserService{ 
 
    // refer to sources 
  @Override 
  public Set<String> getuserRoles(String username) { 
    Map<String, Set<String>> roles = new HashMap<>(); 
...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image