【发布时间】:2017-07-07 01:07:04
【问题描述】:
我有网络应用程序,我想保护我的其余 API,我遵循一些教程,我成功地实现了其余 API 是安全的。
但是当我第一次调用这些页面时,当我在我的网络应用程序中添加 html 文件时,它会向我显示要输入的登录区域。
我只想保护其余的 API 而不是所有的网络应用程序
就我而言,这是我的 application.properties
spring.datasource.url=jdbc:mysql://localhost/geekycoders_myteam
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
logging.level.org.springframework.boot.autoconfigure.security=INFO
security.user.name=admin
security.user.password=admin
这是我的SecurityConfig 课程
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.POST, "/api/**").authenticated()
.antMatchers(HttpMethod.PUT, "/api/**").authenticated()
.antMatchers(HttpMethod.DELETE, "/api/**").authenticated()
.antMatchers(HttpMethod.GET, "/api/**").authenticated()
.anyRequest().permitAll()
.and()
.httpBasic().and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}
这是一个示例控制器
@RestController
@RequestMapping("/api/user")
public class UserController {
@Autowired
UserRepository userRepository;
@RequestMapping("/findall")
@ResponseBody
public List<User> findall(){
return userRepository.findAll();
}
@RequestMapping("/find")
@ResponseBody
public User getUser(@PathParam("id") int id){
return userRepository.findOne(id);
}
}
我将 index.html 放入目录 webapp 一些帮助
【问题讨论】:
标签: java spring spring-boot spring-security