gpt4 book ai didi

java - Spring Security 4 和 REST 的问题,不显示 HTML 文件

转载 作者:行者123 更新时间:2023-12-02 04:07:09 24 4
gpt4 key购买 nike

我的项目有问题,我尝试使用 Spring REST 配置 Spring Security 4,但是当我尝试使用任何上下文访问时出现问题。

这是我的项目结构:

enter image description here

“ View ”内部是我的 html 页面。

这是我的 Spring 配置。

<import resource="classpath:applicationContext-business.xml"/>
<mvc:annotation-driven />


<security:http auto-config="true" >

<security:intercept-url pattern="/" access="permitAll" />
<security:intercept-url pattern="/home" access="permitAll" />

<security:form-login login-page="/iniciar_sesion"
username-parameter="email"
password-parameter="password"
authentication-failure-url="/Access_Denied" />
<security:csrf/>

</security:http>
<context:component-scan base-package="turing.solutions.dy.web" >
<context:include-filter type="regex" expression=".*\.(.)*"/>
<context:exclude-filter type="regex" expression="security"/>
</context:component-scan>

<bean id="customUserDetailsService" class="turing.solutions.dy.web.security.CustomUserDetailService" />

<security:authentication-manager>
<security:authentication-provider user-service-ref="customUserDetailsService"/>
</security:authentication-manager>

还有我的 web.xml

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dy</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>


<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<session-config>
<session-timeout>
10
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

这是我的 RestController:

@RestController
public class LoginController {

@RequestMapping(value = { "/", "/home" }, method = RequestMethod.GET)
public String homePage() {
System.out.println("Redirect");
return "index";
}

@RequestMapping(value="iniciar_sesion",method = RequestMethod.GET)
public String iniciarSession(ModelMap model){
model.put("login", "log");
return "iniciar_sesion";
}

@RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json")
public Map<String, Object> login() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("login", "ok");
return map;
}

@RequestMapping(value = "/Access_Denied", method = RequestMethod.GET)
public String accessDeniedPage(ModelMap model) {
model.addAttribute("user", getPrincipal());
return "accessDenied";
}

private String getPrincipal() {
String userName = null;
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

if (principal instanceof UserDetails) {
userName = ((UserDetails) principal).getUsername();
} else {
userName = principal.toString();
}
return userName;
}

}

这是我的 CustomUserDetailService

@Service("customUserDetailService")
public class CustomUserDetailService implements UserDetailsService {

@Autowired
private UsuariosService usuariosService;

@Override
public UserDetails loadUserByUsername(String correo) throws UsernameNotFoundException {
Usuarios usuario = this.usuariosService.findByCorreo(correo);
if (usuario == null) {
throw new UsernameNotFoundException("El usuairo " + correo + " no existe, favor de verificar");
}
return new User(usuario.getEmail(), usuario.getPassword(), usuario.getActivo(), true, true, true, getGrantedAuthorities(usuario));
}

private List<GrantedAuthority> getGrantedAuthorities(Usuarios usuario) {
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
for (Roles rol : usuario.getRolesList()) {
System.out.println("Usuario " + usuario.getEmail() + " ROl" + rol.getDescRol().toUpperCase());
authorities.add(new SimpleGrantedAuthority("ROLE_" + rol.getDescRol().toUpperCase()));
}
return authorities;
}
}

当我尝试访问 URL“http://localhost:9080/DespreocupateYA/home”时,我看到了这个

enter image description here

但我应该看到这个

enter image description here

所以,我的问题是:为什么我看不到项目中的 html 页面?我搜索了很多Spring配置,但一直无法解决问题。

我的服务器是 Apache Tomcat 8.0.28。

希望您能帮助我,谢谢。

最佳答案

将您的@RestController注释更改为@Controller,它将正常工作。

Here是解释这两个注释之间差异的文档

此外,当您说 RestController 时,您指的是处理 JSON 对象等数据的 Controller 。这里你需要一个简单的 Controller 来处理 html View 。

编辑

您没有提到您正在使用哪种 View 渲染引擎,这是jsp的示例配置

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>

这对于您的 spring 配置文件来说,请确保声明“http://www.springframework.org/schema/beans” namespace ,以便您可以使用 bean 定义

关于java - Spring Security 4 和 REST 的问题,不显示 HTML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34166801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com