346 Replies
JavaBot
JavaBot3y ago
This post has been reserved for your question.
Hey @未来! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Unique index or primary key violation: "PUBLIC.UK_6DOTKOTT2KJSP8VW4D0M25FB7_INDEX_4 ON PUBLIC.USERS(EMAIL NULLS FIRST) VALUES ( /* 1 */ '' )"; SQL statement: insert into users (created, email, password, username, id) values (?, ?, ?, ?, ?) [23505-214] what's line 24 of your UserDetailsServiceImpl? saving a user?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
How are you creating IDs? oh, using uuid
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
what's the no-args constructor?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
Do you have a no-args constructor?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
what's the first line in that image?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
the exact thing
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
there's your problem the test user has the E-Mail "" anon has the E-Mail ""
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
the E-Mail cpöumn is marked with a unique constraint column*
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
Are you adding pastes anywhere?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
did you do it anywhere before?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
are there any pastes in your DB?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
maybe try removing them for testing
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
yeah you could try using @Transactional where saving it but first stop the application, delete the pastes and then check whether it works
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
oh ok
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
it majes sure that a method runs in a transaction so the exception occurs when logging in?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
can you show the code?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
login are there any pastes in the DB when the exception occurs?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
what method does it happen in?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
Does the exception occur if there are no test pastes?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
where in the login code does the exception occur?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
what's line 30? ohhhhhhh is it a StackOverflowError?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
ohhh I see
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
in your user class, add @EqualsAndToString(exclude="pastes") printing a user also tries printing their pastes...
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
@ToString
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
that's what I meant
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
the list is loaded lazily so accessing it does a DB request
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
which fails for some reason probably because it doesn't run in a transaction or something
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
you can mark some methods to run in a transaction
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
if a method runs multiple DB queries, you can use transactional to make sure these run "together"
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
but for the login - don't access/print the pastes
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
ig
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
you know what a StackOverflowError is?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
is there something that's repeating over and over?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
I meant in the stack trace
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
Are there a few lines in the stack trace that repeat over and over?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
I think I know what the issue is but let's see When does this happen? When executing a REST endpoint?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
which endpoint?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
ok
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
I assume the controller code executes successfully and the exception occurs after it?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
but before the response is sent back
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
Do you know what Jackson does?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
serializes and deserializes
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
so it takes your paste and is responsible for converting the object to json that's where (I think) the exception occurs so think about this scenario: You have 3 classes: A, B, C A contains a B field and Bcontains a C field
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
if you serialize that to JSON, what would that look like?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
what would you expect the json to look like?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
yes Now imagine you have two classes A and B A has a B field and B has an A field a is an object of class A
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
b is an object of class B
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
a.b=b b.a=a What would the json look like?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
yes this is exactly what happens
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
Do you know what you can do against that probablem? problem*
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
if you have a StackOverflowError related to equals, hashCode, toString or serialization, it's likely this problem like 99.5% if the equals/hashCode/toString/serialization is the repeating stuff in the stack trace The solution to this kind of problems is to break the circle
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
kinda with annotations but I want you to understand it
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
so if A refers to B and B refers to A, you need to make sure that when serializing A, it doesn't serialize B OR that when it serializes B, it doesn't serialize A with it, right?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
like you are serializing one you don't need the other one to be part of the json, right?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
well you basically say "when serializing A, don't include B`
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
but Jackson also provides an alternative if that isn't feasible
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
this is one of the 4 answers I mentioned
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
ahhhh did it not send my messages?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
I linked to that question and said the first 4 answers give you 4 approaches
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
Sorry I was reading through this and wanted to interject. You can also just have the Principal injected into the Function, like
@PostMapping("/paste")
Paste postPaste(@RequestBody PasteDto payload, Principal principal) {
String title = payload.title().isBlank() ? "Untitled" : payload.title();
Paste paste = new Paste(title, payload.text(), payload.lang());
if (principal != null) {
User user = this.userRepo.findByUsername(principal.getName());
paste.setUploader(user);
user.addPaste(paste);
}
System.out.println(paste.getId());
this.pasteRepo.save(paste);
return paste;
}
@PostMapping("/paste")
Paste postPaste(@RequestBody PasteDto payload, Principal principal) {
String title = payload.title().isBlank() ? "Untitled" : payload.title();
Paste paste = new Paste(title, payload.text(), payload.lang());
if (principal != null) {
User user = this.userRepo.findByUsername(principal.getName());
paste.setUploader(user);
user.addPaste(paste);
}
System.out.println(paste.getId());
this.pasteRepo.save(paste);
return paste;
}
I was getting PTSD flashbacks from a project at work that abused getting the Request (And the response) in the controller.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
JavaBot
JavaBot3y ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
dan1st
dan1st3y ago
- @JsonIgnore - complete ignore a field for (de)serialization (ignore A.b) - giving identifiers to objects and referencing them - telling jackson that a field is holding backrefs - ignoring a field of a field (ignore A.b.a)
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
if you read the 4 most upvoted answers, you should see these four approaches if you don't need the field, I'd use the first approach tbh don't inject Principal, use Authentication instead
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
Authentication is a subclass from Principal
Crain
Crain3y ago
As long as I can slowly get more people to not inject the Request/Response, I'm happy.
dan1st
dan1st3y ago
Principal is from javax.security while Authentication is from Spring you mean HttpServletRequest/HttpServletResponse?
Crain
Crain3y ago
Yes
dan1st
dan1st3y ago
I mean, I am doing that for some cases (mainly content types or accessing weird stuff)
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
because servlets are a bit annoying
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
although ServletContext should be sufficient for that
Crain
Crain3y ago
It can easily be an anti-pattern. I've mainly dealt with it where people were just extracting JSON from the request body, and setting JSON in the response Body. If you have a reason to need it that Spring doesn't support, go for it. But it can easily just do what Spring can already do for you.
dan1st
dan1st3y ago
yes it was created before generics were a thing
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
I typically save both sides but idk whether this is considered a good thing
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
like adding userRepo.save(user) not sure whether that's a good idea though
Crain
Crain3y ago
It's eh, since Hibernate will bundle the queries anyways. Normally you only need the owning side to .save()
dan1st
dan1st3y ago
but when running it with debug output, it doesn't seem to do multiple SQL queries
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
because I don't trust it
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
How exactly do you define owning side? probably no
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
unless you need it because - hibernate
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
if you have multiple DB operations that should go "together"
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
like if the last one fails, should the previous ones be rolled back (all or nothing)?
Crain
Crain3y ago
Normally the Table that owns the Foreign* Key. In the case of a @ManyToMany, either works.
dan1st
dan1st3y ago
and should other things see intermediate states? ah that, yeah
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
In my experience, the important thing is to update the Java objects on both sides because the objects might be cached and Sets with JPA are annoying
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
don't start persisting sets unless you really have a reason to
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
It should be a slightly more optimal query since Hibernate doesn't need to Join the child object. But Honestly if you're using an ORM performance is a second thought.
dan1st
dan1st3y ago
xd enterprise code be like
Crain
Crain3y ago
Persist a list on the Repo Level, use a Set on the Domain level. You'll save a headache.
dan1st
dan1st3y ago
actually, do you know how exactly hibernate performance works with native-image? because it does the magic at compile-time with Spring 6 yes
Crain
Crain3y ago
I haven't played much with Native Image, been meaning to because I'm interested in how it handles Reflection.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
you have a JSON file specifying all classes that are used reflectively or with JNI
Crain
Crain3y ago
The "Domain" object is normally the Service layer. DTO - Controllers Domain - Service Entity/Model - Repository.
dan1st
dan1st3y ago
also all resource files except the ones detected by native-image
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
or use Spring's APIs lol and for libraries - there's GraalVM reachability metadata which is a metadata repository with that stuff for popular libraries not supporting it
Crain
Crain3y ago
I'm surprised they use a JSON file. It's compiled so they might as well use a more optimal format. Unless it just gets read and cached at runtime.
dan1st
dan1st3y ago
the json file is only needed at compilation it tells native-image what's used
Crain
Crain3y ago
AH
dan1st
dan1st3y ago
it's a config file for native-image one json file for JNI, one for reflection, one for resources
Crain
Crain3y ago
Ew. I'm glad Memory isn't a concern where I work.
dan1st
dan1st3y ago
I wrote these json files last week - Swing is annoying
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
Oh that's smart. I hadn't thought about bundling Swing through GraalVM for end users.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
Got a github link? I'm in the mood for a project review.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
not that funny when Swing JNI stuff is missing for libs
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
Yeah Swing is just a JNI wrapper
dan1st
dan1st3y ago
or when GraalVM still has problems with dynamically linking swing .so files on Linux
Crain
Crain3y ago
Alright, quick glance stuff. Your .gitignore is missing .idea and .mvn, should add both of those and remove the files from the repository. Along with the mvnw and mvnw.cmd files
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
yeah I wanted to mention that as well but I searched for the username to better explain why
Crain
Crain3y ago
Normally no, since those are IDE generated files.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
in many cases, the username can be found in the .idea directory of GH repository If people include that
Crain
Crain3y ago
You should prefer Constructor Injection over Field Injection.
@RestController
@RequiredArgsConstructor
public class PasteController {
private final PasteRepository pasteRepo;
private final UserRepository userRepo;
@RestController
@RequiredArgsConstructor
public class PasteController {
private final PasteRepository pasteRepo;
private final UserRepository userRepo;
You can do this using final fields and Lombok's @RequriedArgsConstructor. This is better for avoiding Circular Dependencies, and Testing.
dan1st
dan1st3y ago
like the username of the account you are logged in on your computer
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
as part of your home directory
JavaBot
JavaBot3y ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Crain
Crain3y ago
A lot of people are bad at security.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
GitHub
uwupaste/SecurityConfig.java at master · mirai42/uwupaste
Contribute to mirai42/uwupaste development by creating an account on GitHub.
dan1st
dan1st3y ago
I have also seen GH repos with the username present in multiple files and the authors not wanting their username to be visible
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
http().csrf().disable() + formLogin() I mentioned that previously that's dangerous cause CSRF attacks
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
No, you have to remove it manually.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
the more you procrastinate csrf the harder it gets
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
Yeah. I think there is a Git command to do it without deleting the file, but I don't know.
dan1st
dan1st3y ago
it stores the session in cookies
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
and cookies are the critical thing about CSRF
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
You've blended together some of your model files. I'd recommend separating them out into Entity / Domain packages within your Model Package, and likely stick your Dto Package into there as well.
dan1st
dan1st3y ago
git rm --cached and -r for dirextories
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
--cached makes it not remove them from disk, only from the staging area
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
you still need to commit and push them
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
and even that doesn't completely remove it
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
Entity -> Maps to Tables in a Database. Domain -> Logic stuff.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
Anything not Database.
dan1st
dan1st3y ago
btw if you use records for DTOs, you don't need lombok for them
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
Something like you Language Class, which doesn't seem to be in the Database, but is next to your Database Objects.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
Yeah I need to dig into what you're using HttpStatusCode for, I don't think you need it the way you are. Here is a quick rewrite that's a little bit closer to what I'd at least prefer. At the very least you want to use logging instead of Console Printing.
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
private UserRepository repo;
private PasswordEncoder encoder;

@Autowired
public UserDetailsServiceImpl(UserRepository repo, PasswordEncoder encoder) {
this.repo = repo;
this.repo.save(new User("user", "email@email.com", encoder.encode("password")));
this.repo.save(User.anon);
}

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return Optional.ofNullable(this.repo.findByUsername(username))
.map(e -> {System.out.println(e); return e;})
.map(CustomUserDetails::new)
.orElseThrow(() -> new UsernameNotFoundException(username));
}
}
// Bit weird, rewrite to this.
@Slf4j
@Service
public class UserDetailsServiceImpl implements UserDetailsService { // This is good. 10/10 for actually making a UserDetailsService
private final UserRepository repo;
private final PasswordEncoder encoder;

public UserDetailsServiceImpl(UserRepository repo, PasswordEncoder encoder) {
this.repo = repo;
this.encoder = encoder;
createUsers();
}

private void createUsers() { // Do setup in a seperate function so your Constructor has a single purpose, and this function has a single purpose.
repo.save(new User("user", "email@email.com", encoder.encode("password")));
repo.save(User.anon);
}

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
var user = Optional.ofNullable(this.repo.findByUsername(username)).orElseThrow(() -> new UsernameNotFoundException(username)); // Remove the Maps since we want to log.
log.debug("User Found in the Database: {}", e); // Prefer Logs over Console Prints.
return new CustomerUserDetails(e);
}
}
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
private UserRepository repo;
private PasswordEncoder encoder;

@Autowired
public UserDetailsServiceImpl(UserRepository repo, PasswordEncoder encoder) {
this.repo = repo;
this.repo.save(new User("user", "email@email.com", encoder.encode("password")));
this.repo.save(User.anon);
}

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return Optional.ofNullable(this.repo.findByUsername(username))
.map(e -> {System.out.println(e); return e;})
.map(CustomUserDetails::new)
.orElseThrow(() -> new UsernameNotFoundException(username));
}
}
// Bit weird, rewrite to this.
@Slf4j
@Service
public class UserDetailsServiceImpl implements UserDetailsService { // This is good. 10/10 for actually making a UserDetailsService
private final UserRepository repo;
private final PasswordEncoder encoder;

public UserDetailsServiceImpl(UserRepository repo, PasswordEncoder encoder) {
this.repo = repo;
this.encoder = encoder;
createUsers();
}

private void createUsers() { // Do setup in a seperate function so your Constructor has a single purpose, and this function has a single purpose.
repo.save(new User("user", "email@email.com", encoder.encode("password")));
repo.save(User.anon);
}

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
var user = Optional.ofNullable(this.repo.findByUsername(username)).orElseThrow(() -> new UsernameNotFoundException(username)); // Remove the Maps since we want to log.
log.debug("User Found in the Database: {}", e); // Prefer Logs over Console Prints.
return new CustomerUserDetails(e);
}
}
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
Probably? The constructor is small enough that doing it manually isn't too much on the boilerplate. https://github.com/mirai42/uwupaste/blob/master/src/main/java/xyz/mirai666/uwupaste/util/Util.java Util Class should have a better name. Think of something like Apache's StringUtils class. Still small, but easier to understand what the Util will contain.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
Also the Util class can use multiline strings because we're in Java 17.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
static Logger logger = LoggerFactory.getLogger(UwUPasteApplication.class); Basically this But you get better support for multiple loggers, so it's preferred.
dan1st
dan1st3y ago
btw what about using that in TemplateController?
No description
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
no need to download a file for extracting information about status codes especially using the resolve method provided by that enum
int code=404;
HttpStatus status=HttpStatus.resolve(code);
String name = status.toString();
int code=404;
HttpStatus status=HttpStatus.resolve(code);
String name = status.toString();
Crain
Crain3y ago
List<Paste> latestPastes = this.pasteRepo.findAll().stream()
.sorted(Comparator.comparing(Paste::getTimestamp).reversed())
.limit(6) // show 6 entries
.toList();
List<Paste> latestPastes = this.pasteRepo.findAll().stream()
.sorted(Comparator.comparing(Paste::getTimestamp).reversed())
.limit(6) // show 6 entries
.toList();
Can simplify this section in the Template Controller https://github.com/mirai42/uwupaste/blob/master/src/main/java/xyz/mirai666/uwupaste/view/TemplateController.java#L71 Weird to call Stream Support and Spliterator directly, easier to just get the Stream for the underlying List implementation.
JavaBot
JavaBot3y ago
List<Paste> latestPastes = StreamSupport.stream(this.pasteRepo.findAll().spliterator(), false)
List<Paste> latestPastes = StreamSupport.stream(this.pasteRepo.findAll().spliterator(), false)
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
Wtf
dan1st
dan1st3y ago
it's not a list yeah
Crain
Crain3y ago
Oh, use JpaRepository
dan1st
dan1st3y ago
just checked the javadoc and saw the same thing
Crain
Crain3y ago
I really don't know what Value CrudRepository has over JpaRepository
dan1st
dan1st3y ago
I really need to download Spring JPA javadocs on my mobile lol
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
or just explicitly define
List<Paste> findAll();
List<Paste> findAll();
Crain
Crain3y ago
JpaRepository implements more Interfaces, including the CrudRepository, but it overrides the findAll to return a list.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
Also put your Repositories into a repo or repository package.
dan1st
dan1st3y ago
Ha I got the artifact coordinates of Spring JPA correct on my first attempt
Crain
Crain3y ago
And maybe your TemplateController into the api package. But I've never dealt with a Thymeleaf HTML controller. I've only used it to create PDFs. You know when you say stuff like that I worry for you.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
it's just org.springframework.data for the group id and spring-data-jpa for the artifact id
Crain
Crain3y ago
Oooh you meant that. I'm worried less I have that memorized as well. Yeah that's why I said I'm not 100% sure what the convention is there. Since I've only use Thymeleaf as an HTML generator for other stuff, not as a webpage generate.
dan1st
dan1st3y ago
what else did you think about?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
I honestly thought there was some weird documentation URL. And the "coordinate" was just a term I hadn't heard about. I always expect for there to be something weird I don't know about.
dan1st
dan1st3y ago
nah I don't wannt clientmods
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
maven calls group id/artifact id/version artifact coordinates although technically I only need group and artifact id
Crain
Crain3y ago
I need to learn more maven stuff but that feels like an abyss I don't want to get into.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
I think I'm good on the comments for the Project. All in all, 7/10 from a realistic perspective, 5/10 from a professional standard, not bad. Mostly just structure/naming issues that make it hard to navigate. The code is clean, the functions are (mostly) small, and the generate idea is clean. Extra points docked in Professionalism due to a poor read me, missing tests, structure/naming, and returning an Entity in your Rest Controllers.
dan1st
dan1st3y ago
You know how I look up artifacts? I directly enter the maven central url in my browser
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
Most people in industry aren't held to professional standards, trust me it's good to learn them. I'll tack on 1 point (8/10, 6/10) for being in high school.
dan1st
dan1st3y ago
YES it's correct
Crain
Crain3y ago
See now I'm back to worrying about you.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
JavaBot
JavaBot3y ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
Returning Entities exposes your Database. It's a security concern. Beyond that it often can lead to infinite loops when defining the JSON. Returning Entities is annoying, but acceptable. The bigger issue is when you accept Entities into your Controllers.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
Yep!
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
A PasteDto could work, or if that model doesn't work then make a new PasteResponseDto The main thing is to avoid returning Database representations in your Controller.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
It's the JSON body response to the request. (Or whatever format is set to be returned, we just assume JSON)
dan1st
dan1st3y ago
not as annoying when you have records tbh
Crain
Crain3y ago
Do you use Records for your Database? I've been using Classes still, but then Records for DTOs.
dan1st
dan1st3y ago
like I ofzen just define the DTO right in the rest controller for DTOs
Crain
Crain3y ago
Yeah for DTOs records + some Jakarta Validation is beautiful.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
but saving records to a DB should work with Spring - although not with JPA
Crain
Crain3y ago
Just make it a void type.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
but CrudRepository/Spring Boot automatically detects it or should
Crain
Crain3y ago
@Controller
public class MyController {

public ResponseEntity<String> myString() {
return ResponseEntity.ok("foo");
}

@ResponseBody
public String myResponseString() {
return "bar";
}
}
@RestController
public class MyRestController {
public ResponseEntity<String> myString() {
return ResponseEntity.ok("foo");
}

public String myResponseString() {
return "bar";
}
}
@Controller
public class MyController {

public ResponseEntity<String> myString() {
return ResponseEntity.ok("foo");
}

@ResponseBody
public String myResponseString() {
return "bar";
}
}
@RestController
public class MyRestController {
public ResponseEntity<String> myString() {
return ResponseEntity.ok("foo");
}

public String myResponseString() {
return "bar";
}
}
These are all the same. @RestController just appends @ResponseBody to all the inner functions. Which means ResponseEntity<Void> == void, assuming you don't have dynamic headers to set.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
Shoot.
dan1st
dan1st3y ago
that can't work that should be a compiler error
Crain
Crain3y ago
Fixed Forgot to do the actual call.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
also @RequestMapping?
Crain
Crain3y ago
Yeah, 1/2 the time ResponseEntity is really the last thing to reach for. You can set static headers/response statuses using Annotations. It's really for dynamic responses you want to use it. shhh
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
I made that quickly, I can't link my nice confluence page that's behind a corporate firewall.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
I probably should knab my pages as a PDF for #share-knowledge though. I'll do that tomorrow.
dan1st
dan1st3y ago
:thumbs: although maybe text is a better format than pdf tbh
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
I'll knab a screenshot. Just have a to boot up a VM and hook into the VPN, thankfully Work doesn't ask why I'm using it on a Sunday.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
JavaBot
JavaBot3y ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
dan1st
dan1st3y ago
oh and if you ever plan to use native-image - I hope you have tests for everything and are able to run the tests with native-image xd
Crain
Crain3y ago
The reason for PDF is that they're formatted. (With Headers, as Discord doesn't support them) So doing raw text would ruin the formatting.
No description
dan1st
dan1st3y ago
Discord does allow some of them in forum channel post initial messages
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
No description
dan1st
dan1st3y ago
markdown heading in Discord
Crain
Crain3y ago
Ooh! I'll have to tackle this tomorrow. I think I have some work stuff in these articles, so I'll want to scrub that first.
dan1st
dan1st3y ago
would be nice to have it here
Crain
Crain3y ago
Yeah, I view them as condensed Baeldung Articles.
dan1st
dan1st3y ago
Baeldung is Stack Overflow for Spring as articles
Crain
Crain3y ago
IT'S SO GOOD
dan1st
dan1st3y ago
xd
Crain
Crain3y ago
Does this also mean I can finally get the Expert Role 👉 👈
dan1st
dan1st3y ago
If you want that role: #roles
Crain
Crain3y ago
I'm really sorry to hijack the help thread. I've applied, just never heard back.
dan1st
dan1st3y ago
oh right - that might happen responding to stuff is not our good side lmao
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
we might be /dev/null sometimes
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
everything on that site is amazing like actually everything
We know that everyone cares about their privacy these days. We care about your privacy concerns, and guarantee all of your data will be completely discarded[1]. [1] Except for cases where it will be stored indefinitely to ensure compliance with government regulations[2]. [2] When we say ‘government’ we mean NSA, CIA, FBI, TSA, Communist Party of China (CPC), Nestle, The Coca-Cola Company, the FSB, some of your coworkers and our friends (especially if there is something funny).
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Crain
Crain3y ago
No description
dan1st
dan1st3y ago
yes
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
delete a random file lists all files on the computer, shuffles them, picks the first one, deletes it
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
it just lists all files recursively
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st3y ago
good night or could I already say good morning? After all, it's already 3am in my timezone lol
JavaBot
JavaBot3y ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
JavaBot
JavaBot3y ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one.

Did you find this page helpful?