Skip to content
Open
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
dc243f0
Add new section to Authentication on User Owned Data
brclark Oct 24, 2023
7204f6e
Add section for DTOs & Services to Authentication
brclark Oct 26, 2023
490198c
Move PasswordEncoder to a new configuration and refactor User
brclark Nov 14, 2023
31e8cdc
Add text description for EventService and EventCategoryService
brclark Nov 9, 2023
7bf6a31
Add missing methods in UserService description
brclark Nov 14, 2023
7f0373c
Add text walkthrough for User Roles & Privileges
brclark Nov 13, 2023
cad1240
Add InitialDataLoader description to User Roles & Privileges
brclark Nov 14, 2023
f365c41
Move User Roles & Privileges walkthrough to Authentication Next Steps
brclark Nov 14, 2023
2e5304d
Add SecurityService to User Roles & Privileges
brclark Nov 24, 2023
a1c049e
Add Spring Security 6 instruction set for Authentication Bonus
brclark Dec 7, 2023
3ff0e31
Add Role Based Access instructions
brclark Dec 8, 2023
1688ff5
Fixes part 1 - going throuh the User Owned Data section
brclark Dec 12, 2023
31af88b
Edit the add-service-dto section with some updates from review
brclark Dec 12, 2023
18a3484
Fixes from user-roles-privileges proofread
brclark Dec 13, 2023
d8280a1
Spring Security fixes from proofread
brclark Dec 14, 2023
4561a4a
Add fixes from peer review
brclark Dec 21, 2023
6425520
Updates from testing and tutorial making
brclark Dec 30, 2023
11c3dd6
Clean up intro to bonus module, Tag instructions, and code fixes
brclark Jan 9, 2024
01748af
Remove mentions of video lessons from spring security bonus module
brclark Jan 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions content/authentication/reading/add-service-dto/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,57 @@ public class EventCategoryDTO {
}
```

### Prepping `User` model for `UserService`

We have a few updates we need to make to the `User` model to prep it for use
with the `UserService`. Namely, we need to move the `PasswordEncoder` class to
its own managed config. Our password encoder is currently a static instance
in the `User` model, but we will need access to our encoder within the
`UserService` so that we can validate a login password against the user's
encrypted password.

#### Creating `PasswordEncoder` bean

This password encoder object will be a managed Java bean, similar to a
controller, that can be referenced using an `@Autowired` field.

First, create a new package `config` within your `codingevents` package. Then,
create a new class `EncoderConfig` in the package.

```java
@Configuration
public class EncoderConfig {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
```

The `@Configuration` annotation tells Spring that this class will contain
`@Bean` definitions for Spring managed objects. Inside the class, we
define a `@Bean` that will return an instance of the `BCryptPasswordEncoder`
that we were using the `User` class.

#### Refactoring `User` model

Next, we need to modify our `User` model for use of the `PasswordEncoder`.
We are going to rework our constructor so that a new `User` instance gets
the encoded password passed in, and the `User` object will not be responsible
for doing any encoding.

Remove the field containing the `static final BCryptPasswordEncoder`.

Modify the `User` constructor so that it takes in `String pwHash` as an argument
and uses it to set the field directly, removing the call to `encode`.

Lastly, remove the `isMatchingPassword` method and replace it with a getter for
Comment thread
brclark marked this conversation as resolved.
Outdated
the `pwHash` field.

Our `User` class is now refactored. Instead of having the `User` class be
responsible for encoding passwords, we will do password encryption in the
`UserService` and pass encrypted passwords to new `User` instances.

### Adding `UserService`

The reponsibilities of the service layer are to translate DTOs to Models
Expand Down