jpa - spring-data-rest and spring security circular reference when using security annotations in user repository -
jpa - spring-data-rest and spring security circular reference when using security annotations in user repository -
i got problem custom userdetailservice , circular reference. add together @preauthorize annotation customerrepository circular reference error. it's clear has happen cause userdetailservice uses well. question how can solve circular reference? using spring-data-rest , spring-security.
code below should illustrate needed reproduce error
package demo; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.commandlinerunner; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.enableautoconfiguration; import org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.configuration; @configuration @componentscan @enableautoconfiguration public class springtestapplication implements commandlinerunner{ @autowired private customerrepository customerrepository; public static void main(string[] args) { springapplication.run(springtestapplication.class, args); } @override public void run(string... args) throws exception { client admin = customerrepository.save(new customer("demo", "1234")); } } -
package demo; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.annotation.configuration; import org.springframework.security.config.annotation.authentication.builders.authenticationmanagerbuilder; import org.springframework.security.config.annotation.method.configuration.enableglobalmethodsecurity; import org.springframework.security.config.annotation.web.builders.httpsecurity; import org.springframework.security.config.annotation.web.configuration.enablewebsecurity; import org.springframework.security.config.annotation.web.configuration.websecurityconfigureradapter; @configuration @enableglobalmethodsecurity(prepostenabled = true) @enablewebsecurity public class securityconfiguration extends websecurityconfigureradapter{ @autowired private demouserdetailservice demouserdetailservice; @override protected void configure(authenticationmanagerbuilder auth) throws exception { auth.userdetailsservice(demouserdetailservice); } @override protected void configure(httpsecurity http) throws exception { http.authorizerequests().anyrequest().fullyauthenticated(); http.httpbasic(); http.csrf().disable(); } } -
package demo; import java.util.arrays; import org.springframework.beans.factory.annotation.autowired; import org.springframework.security.core.authority.simplegrantedauthority; import org.springframework.security.core.userdetails.user; import org.springframework.security.core.userdetails.userdetails; import org.springframework.security.core.userdetails.userdetailsservice; import org.springframework.security.core.userdetails.usernamenotfoundexception; import org.springframework.stereotype.service; @service public class demouserdetailservice implements userdetailsservice { private final customerrepository customerrepository; @autowired public demouserdetailservice(customerrepository customerrepository){ this.customerrepository = customerrepository; } @override public userdetails loaduserbyusername(string username) throws usernamenotfoundexception { client customer = customerrepository.findbyusername(username).get(); boolean enabled = true; boolean accountnonexpired = true; boolean credentialsnonexpired = true; boolean accountnonlocked = true; homecoming new user( customer.getusername(), customer.getpassword(), enabled, accountnonexpired, credentialsnonexpired, accountnonlocked, arrays.aslist(new simplegrantedauthority("role_admin")) ); } } -
package demo; import java.util.optional; import org.springframework.data.repository.pagingandsortingrepository; import org.springframework.security.access.prepost.preauthorize; public interface customerrepository extends pagingandsortingrepository<customer, long>{ @preauthorize("hasrole('role_admin')") optional<customer> findbyusername(string username); } -
package demo; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.id; @entity public class client { @id @generatedvalue private long id; private string username; private string password; public customer(){} public customer(string username, string password){ this.username = username; this.password = password; } public long getid() { homecoming id; } public void setid(long id) { this.id = id; } public string getusername() { homecoming username; } public void setusername(string username) { this.username = username; } public string getpassword() { homecoming password; } public void setpassword(string password) { this.password = password; } }
use mechanism userdetailsservice, accessing database through jdbc etc. or create sec repository used userdetailsservice , doesn't contain security annotations.
spring jpa spring-security spring-data-rest
Comments
Post a Comment