Cannot read data from Mono [closed] - spring

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 days ago.
This post was edited and submitted for review 8 days ago.
Improve this question
I am trying to validate authentication in reactive spring application with Spring Security. I could not read content from Mono in the controller. It is not emitting any values when I subscribed. I have the following code in the controller:
#Controller
public class TestConroller {
public void test(){
Mono<Authentication> monoAuth=ReactiveSecurityContextHolder.getContext().map(SecurityContext::getAuthentication);
monoAuth.subscribe(authentication->validate(authentication)
}
private void validate(Authentication authentication){
System.out.println(authentication.getPrincipal().getName());
}
The validate method is never called

Related

Spring security filter by authenticated user [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
What is in Spring Boot the best way to filter all queries of entity adding the authenticated user check (using the Authentication Name field)?
Adding a where hibernate on the entity? Is it possible?
Adding Spring Security postFilter? I didn't understand if I should add it to my repository or to my service. Isn't it an inefficient system (than executing a filtered query)?
EDIT
No, I'm not using Spring-Data, but I can add it to my project.
For example, if my repository is:
public interface MyRepository extends JpaRepository<MyEntity, Long>, QuerydslPredicateExecutor<MyEntity>, QuerydslBinderCustomizer<QMyEntity> {
...
}
I have to override all methods of the classes of JpaRepository and QuerydslPredicateExecutor and QuerydslPredicateExecutor that I use in my project? It's correct?
The postFilter is not an efficient option since it would result in fetching many data from the database, but throwing most of them away.
If you're using Spring Data JPA, then you can use the Spring Security data integration and do something like this to include the authentication name in the query (automatically fetched from the SecurityContext).
#Repository
public interface BillingRepository extends JpaRepository<Billing,Long> {
#Query("select b from Billing b where b.userId = ?#{ authentication?.name }")
Ierable<Billing> findBillings();
}

How to upload images to a database with other details using postman [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to upload a image with other details like employee id, employee name etc using spring boot and spring data jpa, trying to send request using postman.I have searched a lot but I cannot find any examples sending both image and other details in one method, as I am a fresher unable to find exact solution and also I don't want to use ObjectMapper to read values.Can some one help me.
Thanks in advance
Check this Example:
#PostMapping("/test")
public Object getConsumedUser(
#Valid User user,
#RequestParam("file") MultipartFile file) {
return file;
}
-----------
class User {
private String name;
private String username;
//Getter and Setter
...
}

Displaying user details after logging in [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am using Spring security for my web application, and I would like to display some of the logged in users' details. In my code, I am trying to use #Autowired on the user itself after logging in and use it as a model to display some of its fields on the frontend side:
#Autowired
public AttractionController(XyService xyService,
ApplicationUser applicationUser) {
this.xyService= xyService;
this.applicationUser = applicationUser;
}
#GetMapping("/")
String main(Model model, #ModelAttribute
Attraction xy) {
model.addAttribute("xy", xyService.findAll());
model.addAttribute("user", applicationUser);
return "main";
I think the problem is not only with the implementation, but also with the approach itself.
How should I solve this problem?
Thank you in advance.
Assuming that ApplicationUser is some kind of data class which holds user information, it is totally pointless to autowire it, since it is more like not controlled by Spring.
In order to get the currently authenticated user, you can simply use SecurityContextHolder. It can be instantiated in the following way:
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
// check if there is somebody authenticated
if (!(authentication instanceof AnonymousAuthenticationToken)) {
String currentUserName = authentication.getName();
// do something the the username, maybe get more information from a database
}

Best way to inject component in Spring with Kotlin [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
For inject a beans with Spring/Kotlin, I know two ways :
Passing it into the constructor :
#Service
open class MyService #Autowired constructor(
#Autowired
val myRepository: MyRepository
)
Using the 'lateinit' keyword :
#Service
open class MyService {
#Autowired
lateinit var myRepository: MyRepository
}
I know the two works, but i'd like to know which one is the best ? Is there some problem I can encounter with one solution and not the other ?
Thank you !
I prefer constructor. Spring no longer requires The #Autowired annotation if there is only one constructor. This way you don't have to make the class open (you do for some Spring things, like #Scheduled but that's another question) or use a var. It's also pretty easy to read.
This is all you need
#Service
class MyService (private val myRepository: MyRepository)

Spring #autowired annotation example [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Can someone explain spring #autowired example which is given in the below tutorials point link?
Whatever id I give in the place of bean id which is "spellChecker" it takes that and injects it. for example instead of bean id="spellChecker" if I give bean id="a" in Beans.xml
Spring auto-wired annotation
Here are some point for autowire functionality and how it is works interally,
1) If your application has two bean for same class then it would not works.for that you have to give same bean name as you given in bean.xml file.
like one bean is -> id = "spellChecker1"
second bean is -> id = "spellChecker2"
now you have to autowired like
bean 1 -> #autowired
private SpellChecker spellChecker1;
bean 2 -> #autowired
private SpellChecker spellChecker2;
2) if your application has only one bean for class then it would automatically detect bean and inject.
In your case application has only one bean id="a" so spring automatically detect that bean for class SpellChecker.

Resources