请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

老师 您好,请问我再学习4-4章结尾的时候写到properties时,项目启动不了了

一直报错: 

BrowserSecurityConfig.Class 代码如下


package com.ginger.security.browser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import com.ginger.security.core.properties.SecurityProperties;
@Configuration
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private SecurityProperties securityProperties;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
.loginPage("authentication/require")
.loginProcessingUrl("/authentication/form")
.and()
.authorizeRequests()
.antMatchers("authentication/require",securityProperties.getBrowser().getLoginPage()).permitAll()
.anyRequest()
.authenticated()
.and()
.csrf().disable();
}
}


SecurityProperties.Class 代码如下

package com.ginger.security.core.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "ginger.security")
public class SecurityProperties {
private BrowserProperties browser =  new BrowserProperties();
public BrowserProperties getBrowser() {
return browser;
}
public void setBrowser(BrowserProperties browser) {
this.browser = browser;
} 
}

运行报错:

Description:Field securityProperties in com.ginger.security.browser.BrowserSecurityConfig required a bean of type 'com.ginger.security.core.properties.SecurityProperties' that could not be found.
Action:Consider defining a bean of type 'com.ginger.security.core.properties.SecurityProperties' in your configuration.

打包报错:

[

ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException


正在回答

2回答

报错是说SecurityProperties这个bean不存在,应该是启动类没扫描到这个类,注意启动类要在组件类的父包里,比如 'com.ginger.security.core.properties.SecurityProperties' 这个类,默认情况下,程序的启动类,一定要在他的父包里,比如 'com.ginger'包里,才能扫描到它。

1 回复 有任何疑惑可以回复我~
  • 提问者 Qolome #1
    不好意思,这几天忙没顾上看 ,启动类是 
    @SpringBootApplication 的类吗?
    
    这个类是 所在包是package com.ginger;
    回复 有任何疑惑可以回复我~ 2018-03-20 13:34:49
  • 提问者 Qolome #2
    非常感谢!
    回复 有任何疑惑可以回复我~ 2018-03-20 14:02:21
提问者 Qolome 2018-03-20 13:35:12
package com.ginger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
 * 
 * @Description: SpringBoot启动 
 * @author 姜锋
 * @date 2018年2月12日 下午4:31:08 
 * @version V1.0   
 *
 */
@SpringBootApplication
@RestController
@EnableSwagger2
public class GingerApplication {
public static void main(String[] args) {
SpringApplication.run(GingerApplication.class, args);
}
@GetMapping("/hello")
public String hello() {
return "hello Spring security";
}
}


0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号