how to construct @service in Springboots using a payload variable
how to construct @service in Springboots using a payload variable
I'm quite new in spring boots so I hope this is not a silly question
I have a @Service that needs to initiate a class attribute, this attribute needs a information that comes from the RestPayload in the Controller. I'm not finding the most recommend way to do that.
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/searchUser")
public List<UserWrapper> searchUser(@RequestBody UserWrapper userWrapper) {
List<UserWrapper> returnUserWrapper = userService.findByName(userWrapper);
return returnUserWrapper;
}
}
And the service layer, I would like to be something like:
@Service
public class UserService {
private LdapTemplate ldapTemplate;
public static final String BASE_DN = "xxxxxxx";
@Value( value = "${sample.ldap.url}" )
private String ldapUrl;
@Value( value = "${sample.ldap.base}" )
private String ldapBase;
public UserService() {
}
public UserService(String dn, String password) {
LdapContextSource ctxSrc = new LdapContextSource();
System.out.println(this.ldapUrl);
ctxSrc.setUrl(ldapUrl);
ctxSrc.setBase(ldapBase);
ctxSrc.setUserDn(dn);
ctxSrc.setPassword(password);
ctxSrc.afterPropertiesSet(); // this method should be called.
this.ldapTemplate = new LdapTemplate(ctxSrc);
}
The String dn and String password will come in the REST Payload but the other properties comes from a properties file.
Hope someone can guide me with best practices
dn
password
setters
1 Answer
1
for ldap authentication you should have a look on spring-security:
on the other hand you can access almost any request parameter by just injecting it via a annotation like in these examples:
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
After your receive your
dn
andpassword
use twosetters
, then call a method that will will call your ldap inside your service.– Dfor Tye
Jul 1 at 2:26