Java 如何使用和不使用springboot身份验证访问页面?
我使用Java 如何使用和不使用springboot身份验证访问页面?,java,mongodb,spring-boot,Java,Mongodb,Spring Boot,我使用Springboot和mongodb上的教程创建了一个简单的项目。目前,我有一个如下页面: 如您所见,这是一个搜索页面。直到昨天,它还没有包括搜索选项,用户只需点击登录,然后搜索一些东西 如果用户单击login按钮并登录系统,他可以搜索项目,结果显示在名为searchResults.html的页面上,没有任何问题。 但是,我需要做的是让用户在第一页使用搜索(页面已更改为上面显示的图片,以包含搜索选项)并查看结果。也就是说,有两个选项:1)用户登录系统,搜索项目并查看结果;2)用户在不登录
Springboot
和mongodb
上的教程创建了一个简单的项目。目前,我有一个如下页面:
如您所见,这是一个搜索页面。直到昨天,它还没有包括搜索选项,用户只需点击登录,然后搜索一些东西
如果用户单击login
按钮并登录系统,他可以搜索项目,结果显示在名为searchResults.html
的页面上,没有任何问题。
但是,我需要做的是让用户在第一页使用搜索(页面已更改为上面显示的图片,以包含搜索选项)并查看结果。也就是说,有两个选项:1)用户登录系统,搜索项目并查看结果;2)用户在不登录系统的情况下搜索内容。
当前的实现是第一部分,而不是第二部分。我不知道该如何更改我的实现。
以下是相关代码(如果我要分享代码的其他部分,请让我知道):
第一页代码:
<body>
<div layout:fragment="content">
<h1>Welcome!</h1>
<form action="/search" method="POST">
<div class="form-group row">
<div class="col-sm-6" id="category_search">
<div class="dropdown">
<div class="input-group justify-content-center">
<div class="input-group-btn">
<button class="btn btn-md dropdown-toggle" type="button"
id="dropdownMenuButton" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">Select a category</button>
<div class="dropdown-menu" onchange="selectMenu1"
aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="#" data="Hotel">Hotel</a> <a
class="dropdown-item" href="#" data="Food">Fast food</a>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-3" id="search_city_id">
</div>
</div>
<div class="form-group row">
<div class="col-sm-3">
<input type="text" name="search" id="search"
class="form-control coloredInput" th:value="${search}" />
</div>
<button type="submit" class="btn btn-primary" value="search">Search</button>
</div>
</form>
</div>
</body>
</html>
非常感谢您的帮助。为您的
/search
端点设置权限:.antMatchers(“/search”).permitAll()
。
其次,对于您的情况,我建议在添加用户详细信息以供查看之前检查用户是否登录控制器。
.antMatchers(“/search”).permitAll()
,然后在添加用户详细信息以供查看之前检查用户是否登录控制器。@Seldo97非常感谢。成功了。你能写下你的评论作为回答吗,这样我就可以接受它作为正确的答案了?没问题。我补充了答案。
@Controller
public class RController {
@Autowired
private CustomUserDetailsService userService;
@RequestMapping("/home")
public String home(Model model) {
model.addAttribute("pointList", rRepository.findAll());
return "home";
}
@RequestMapping(value = "/search")
public ModelAndView search(Model model, @RequestParam("search") String search,
@RequestParam ("search-entityType-value") String searchEntityTypeValue,
@RequestParam("selected_category") String selected_category
) {
List<RAggrResults> result = RSearchRepository.searchRs(search, searchEntityTypeValue, selected_category);
LinkedHashMap<String, List<R>> hashMap = new LinkedHashMap<String, List<R>>();
for (int i = 0; i < result.size(); i++) {
List<R> rs_of_one_group = result.get(i).getRs();
hashMap.put(rs_of_one_group.get(0).getPlace().getPlace_name(), result.get(i).getRs());
}
model.addAttribute("searchResult", hashMap);
model.addAttribute("search", search);
ModelAndView modelAndView = new ModelAndView();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User user = userService.findUserByEmail(auth.getName());
modelAndView.addObject("currentUser", user);
modelAndView.addObject("fullName", "Welcome " + user.getFullname());
modelAndView.addObject("adminMessage", "Content Available Only for Users with Admin Role");
modelAndView.setViewName("searchResults");
return modelAndView;
}
}
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
@Autowired
CustomizeAuthenticationSuccessHandler customizeAuthenticationSuccessHandler;
@Bean
public UserDetailsService mongoUserDetails() {
return new CustomUserDetailsService();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
UserDetailsService userDetailsService = mongoUserDetails();
auth
.userDetailsService(userDetailsService)
.passwordEncoder(bCryptPasswordEncoder);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/login").permitAll()
.antMatchers("/signup").permitAll()
.antMatchers("/dashboard/**").hasAuthority("ADMIN").anyRequest()
.authenticated().and().csrf().disable().formLogin().successHandler(customizeAuthenticationSuccessHandler)
.loginPage("/login").failureUrl("/login?error=true")
.usernameParameter("email")
.passwordParameter("password")
.and().logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/").and().exceptionHandling();
}
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**");
}
}