Spring Boot不使用UserDetailsService(Spring Boot Not Using UserDetailsService)
我正在尝试使用注释为JPA身份验证配置Spring Boot 1.2.5应用程序,它似乎总是使用内存提供程序。
应用程序:
@EnableWebMvc @ComponentScan @EnableAutoConfiguration @SpringBootApplication public class ClubBooksApplication { protected final Logger logger = LoggerFactory.getLogger(getClass()); public static void main(String[] args) { SpringApplication.run(ClubBooksApplication.class, args); } }
WebSecurityConfigurerAdapter
。 我玩过订单,但似乎总是配置内存提供商。 我觉得我可能错过了一段配置,但这种模式与我在搜索中找到的样本相匹配。@Configuration @EnableWebMvcSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) //@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) // After in memory //@Order(SecurityProperties.IGNORED_ORDER) // Before in memory //@Order(SecurityProperties.BASIC_AUTH_ORDER) // Not unique @Order(SecurityProperties.BASIC_AUTH_ORDER - 50) // Before in memory public class WebSecurityConfig extends WebSecurityConfigurerAdapter { private PasswordEncoder passwordEncoder; protected final Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private UserDetailsService userDetailsService; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { logger.info(String.format("configure AuthenticationManagerBuilder: %s", userDetailsService)); super.configure(auth); auth.userDetailsService(userDetailsService) .passwordEncoder(passwordEncoder()); }
这是日志输出。 在显示生成的密码之前,您可以看到它正在配置
UserDetailsService
。 基于我对代码的挖掘,如果没有配置其他提供程序但是设置UserDetailsService
配置DAO提供程序,它似乎只配置内存提供程序。2015-08-20 11:19:24.187 INFO 42332 --- [ost-startStop-1] yConfig$$EnhancerBySpringCGLIB$$c647e8e8 : configure AuthenticationManagerBuilder: com.wstrater.server.clubBooks.server.service.impl.UserLoginDetailServiceImpl@46f0f40a 2015-08-20 11:19:24.226 INFO 42332 --- [ost-startStop-1] yConfig$$EnhancerBySpringCGLIB$$c647e8e8 : passwordEncoder 2015-08-20 11:19:24.410 INFO 42332 --- [ost-startStop-1] b.a.s.AuthenticationManagerConfiguration : Using default security password: 838a7ab0-3bd0-4e87-94ca-de2dfd34b965 2015-08-20 11:19:24.526 INFO 42332 --- [ost-startStop-1] yConfig$$EnhancerBySpringCGLIB$$c647e8e8 : configure HttpSecurity
我在应用程序中包含了Actuator,当我尝试访问
http://localhost:8080/mappings
,尽管配置了基于表单的身份验证,但我仍然提示我使用BasicAuth
。 用户/生成的密码适用于BasicAuth
。 我的UserDetailsService
实现未被调用。配置
HttpSecurity
。 在创建内存提供程序并显示生成的密码后调用此方法,因此我怀疑它是否会影响提供程序配置。 我发现有趣的一件事是,尽管指定了formLogin()
但仍然提示我使用BasicAuth
。 我提出这个问题,因为我也遇到了映射控制器的问题。 我认为这是无关的,但我知道什么。@Override protected void configure(HttpSecurity http) throws Exception { logger.info("configure HttpSecurity"); super.configure(http); http.authorizeRequests() .antMatchers("/", "/public/**") .permitAll() .antMatchers("/rest/**") .authenticated() .antMatchers("/web/**") .authenticated() .anyRequest() .fullyAuthenticated(); http.formLogin() .loginPage("/login") .usernameParameter("userName") .passwordParameter("password") .failureUrl("/login?error") .defaultSuccessUrl("/web/") .permitAll() .and().logout() .logoutUrl("/logout") .logoutSuccessUrl("/") .permitAll() .and().rememberMe(); }
我可以看到我的控制器是由Spring加载的,因为它们是使用
http://localhost:8080/beans
列出的,但我没有看到http://localhost:8080/mappings
。我的登录控制器相当简单。
@Controller @Path("/login") public class LoginWebController { @GET public ModelAndView getLoginPage(@RequestParam(required = false) String error) { return new ModelAndView("login", "error", error); } }
谢谢,韦斯。
I am trying to configure a Spring Boot 1.2.5 application for JPA authentication using annotations and it appears to be always using the in-memory provider.
The application:
@EnableWebMvc @ComponentScan @EnableAutoConfiguration @SpringBootApplication public class ClubBooksApplication { protected final Logger logger = LoggerFactory.getLogger(getClass()); public static void main(String[] args) { SpringApplication.run(ClubBooksApplication.class, args); } }
The
WebSecurityConfigurerAdapter
. I have played around with the order but it always seems to configure the in-memory provider. I feel like I could be missing a piece of configuration but this pattern matches the samples I found in my searches.@Configuration @EnableWebMvcSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) //@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) // After in memory //@Order(SecurityProperties.IGNORED_ORDER) // Before in memory //@Order(SecurityProperties.BASIC_AUTH_ORDER) // Not unique @Order(SecurityProperties.BASIC_AUTH_ORDER - 50) // Before in memory public class WebSecurityConfig extends WebSecurityConfigurerAdapter { private PasswordEncoder passwordEncoder; protected final Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private UserDetailsService userDetailsService; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { logger.info(String.format("configure AuthenticationManagerBuilder: %s", userDetailsService)); super.configure(auth); auth.userDetailsService(userDetailsService) .passwordEncoder(passwordEncoder()); }
Here is the log output. You can see it is configuring the
UserDetailsService
before displaying the generated password. Based on my digging into the code, it appears to only configure the in-memory provider if no other provider is configured but setting theUserDetailsService
configures a DAO provider.2015-08-20 11:19:24.187 INFO 42332 --- [ost-startStop-1] yConfig$$EnhancerBySpringCGLIB$$c647e8e8 : configure AuthenticationManagerBuilder: com.wstrater.server.clubBooks.server.service.impl.UserLoginDetailServiceImpl@46f0f40a 2015-08-20 11:19:24.226 INFO 42332 --- [ost-startStop-1] yConfig$$EnhancerBySpringCGLIB$$c647e8e8 : passwordEncoder 2015-08-20 11:19:24.410 INFO 42332 --- [ost-startStop-1] b.a.s.AuthenticationManagerConfiguration : Using default security password: 838a7ab0-3bd0-4e87-94ca-de2dfd34b965 2015-08-20 11:19:24.526 INFO 42332 --- [ost-startStop-1] yConfig$$EnhancerBySpringCGLIB$$c647e8e8 : configure HttpSecurity
I have included Actuator in the app and when I try to access
http://localhost:8080/mappings
, I am prompted withBasicAuth
despite configuring form based authentication. The user/generated password works forBasicAuth
. MyUserDetailsService
implementation is not called.Configuring
HttpSecurity
. This method is called after the in-memory provider is created and the generated password is displayed so I doubt it impacts the provider configuration. The one thing I find interesting is that I get prompted forBasicAuth
despite specifyingformLogin()
. I bring this up since I am also having issues with mapping my controllers. I think it is unrelated but what do I know.@Override protected void configure(HttpSecurity http) throws Exception { logger.info("configure HttpSecurity"); super.configure(http); http.authorizeRequests() .antMatchers("/", "/public/**") .permitAll() .antMatchers("/rest/**") .authenticated() .antMatchers("/web/**") .authenticated() .anyRequest() .fullyAuthenticated(); http.formLogin() .loginPage("/login") .usernameParameter("userName") .passwordParameter("password") .failureUrl("/login?error") .defaultSuccessUrl("/web/") .permitAll() .and().logout() .logoutUrl("/logout") .logoutSuccessUrl("/") .permitAll() .and().rememberMe(); }
I can see my controllers are being loaded by Spring since they are listed using
http://localhost:8080/beans
but I do not see the mappings inhttp://localhost:8080/mappings
.My login controller is rather simple.
@Controller @Path("/login") public class LoginWebController { @GET public ModelAndView getLoginPage(@RequestParam(required = false) String error) { return new ModelAndView("login", "error", error); } }
Thanks, Wes.
原文:https://stackoverflow.com/questions/32122755
最满意答案
修复了明显的错误后,它为我编译好。
public delegate Y Function<X,Y>(X x); public class Map<X,Y> { private Function<X,Y> F; public Map(Function<X,Y> f) { F = f; } public ICollection<Y> Over(ICollection<X> xs){ List<Y> ys = new List<Y>(); foreach (X x in xs) { X x2 = x;//ys.Add(F(x)); } return ys; } }
After fixing the obvious errors it compiles fine for me.
public delegate Y Function<X,Y>(X x); public class Map<X,Y> { private Function<X,Y> F; public Map(Function<X,Y> f) { F = f; } public ICollection<Y> Over(ICollection<X> xs){ List<Y> ys = new List<Y>(); foreach (X x in xs) { X x2 = x;//ys.Add(F(x)); } return ys; } }
相关问答
更多-
这并没有完全回答你的问题,但仍然想指出这个假设是错误的: 在C#中,我发现没有像C ++或Java的迭代器这样的东西 它在C#中称为枚举器,请参阅IEnumerator
。 还要考虑使用传统for -loop的代码示例: static void Main(string[] args) { var data = new List { 7, 10, 0 }; for (var it = data.GetEnumerator(); it.MoveNext(); ) { ... -
foreach (string s in sList) { if (s.equals("ok")) return true; } return false; 或者,如果您在找到该项目后需要执行其他操作: bool found = false; foreach (string s in sList) { if (s.equals("ok")) { found = true; break; // get out of the loop ...
-
你不需要一个foreach循环 - 只需要使用正则循环,但是反过来,从最后开始并开始。 //For each checked box, run the delete code for (int i = 0; i < this.organizeFav.CheckedItems.Count; i++) { //this *replaces* the foreach loop for(int j = favoritesToolStripMenuItem.DropDownItems.Count - ...
-
C#中的ForEach循环问题(Issue with ForEach Loop in C#)[2023-01-04]
使用Linq的选择: opps.Add(new Opportunity() { DelegateList = orderLine.DelegatesList .Select(orderLineDelegate => new Delegates { FirstName = orderLineDelegate.FirstName, LastName = orderLineDelegate.LastName ... -
C# - 循环foreach直到真(C# - Loop foreach until true)[2023-01-20]
您使用while循环的想法应该可行。 你可以尝试这样的事情: IntPtr hWnd = IntPtr.Zero; bool isFound = false; while(!isFound) { foreach (Process procList in Process.GetProcess()) { if (procList.MainWindowTitle.Contains("SAP Logon")) { isFound = true; hWnd = ... -
你可以在这里使用for循环 : // Pay attention to reversed order: // each currentNode.Remove() changes currentNode.Nodes.Count for (int i = currentNode.Nodes.Count - 1; i >= 0; --i) { TreeNode childNode = currentNode.Nodes[i]; if (!someCondition) { currentNod ...
-
如果数据库中的数据在填充列表中没有大的延迟,并且您没有提到用数据填充列表的问题,为什么不立即创建Point和Placemark对象。 代码如下。 var doc = new Document(); using (SqlConnection conn = new SqlConnection(connstring)) { conn.Open(); SqlCommand cmd = new SqlCommand(select, conn); ...
-
没有办法直接做你想做的事(没有“goto”标签 - 消灭思想!),但是你可以使用“break”关键字,并设置一个你可以参考的变量。 void() { var testWasTrue = false; foreach() { if() { testWasTrue = true; break; // break out of the "foreach" } } if( !t ...
-
C#中的通用foreach循环(Generic foreach loop in C#)[2023-08-28]
修复了明显的错误后,它为我编译好。 public delegate Y Function(X x); public class Map { private Function F; public Map(Function f) { F = f; } public ICollection Over(ICollection xs){ List ys = new List () ... -
我的猜测是,当您通过单步执行每行时,您的代码可能会有不同的行为。 (可能是因为数据库) 确保该方法没有抛出任何异常(放置一个调用Console.WriteLine或MessageBox.Show的catch块,看看是否有任何事情发生)。 查看数据库日志,看看有没有什么有趣的地方。 另外,请发布该方法的完整源代码。 It was not iterating through the foreach loop when I was not debugging the application because the ...